运行./mongo 时 connect time out 27017
开始按照网上的做法删掉了lock文件,或者运行 –repair命令;但都不行。
最后发现运行 ./mongod 时会在
waiting for connections on port 27017
这句话后等待 (此时数据库就已经启动),查看端口已经发现了 27017 ,一直以为有问题,其实这时候运行 ./mongo 即可。
新开一个终端后,再运行 ./mongo (注意没有d) ,此时前一个窗口显示:
[listener] connection accepted from 127.0.0.1:35226 #1 (1 connection now open)
现在就可以使用mongodb数据库了
后台启动MongoDB
上面的方法启动后是前台方式,必须要有终端,否则关闭终端就会关闭数据库。这当然不行,所以我们需要在后台启动。
后台启动MDB:
- 使用–fork参数启动
mongod --fork --logpath <log_file_path> --dbpath <db_folder_path>
--fork 将mongod作为后台服务启动,并必须要跟随 --logpath 指定日志文件路径,如果该日志文件不存在会被自动创建
--dbpath 数据的存储目录
关闭MDB服务:
- 使用shutdownServer()
# mongo // 从linux命令行进入mongod命令行
> use admin // 切换到管理员模式
> db.shutdownServer() // 关闭mongodb服务
- 使用 —shutdown
也可以使用–shutdown进行关闭。但要指定–dbpath,因为一台机器上可以运行着多个mongodb实例,避免误杀。
mongod --shutdown --dbpath <db_folder_path>
- 使用 kill 命令
在Linux机器上也可以通过ps aux | grep mongo过滤出mongodb服务并找出对应的进程id后,使用kill命令进行关闭。
kill <mongod process ID>
kill -2 <mongod process ID>
但不要使用kill -9 (i.e. SIGKILL).
kill -9命令迫使进程在运行时突然终止,进程在结束后不能自我清理。可能导致系统资源无法正常释放,一般不推荐使用,除非其他办法都无效。前台进程可以使用 Ctrl+C
当没有使用–fork参数时,为前台进程,可以直接使用Ctrl+C关闭,或者直接关闭远程会话。
无法远程连接MDB
当你启动MDB时看到这条警告:WARNING: This server is bound to localhost 请注意你将只能在本地连接MDB,后面也给了提示
Remote systems will be unable to connect to this server.
Start the server with --bind_ip <address> to specify which IP
addresses it should serve responses from, or with --bind_ip_all to
bind to all interfaces. If this behavior is desired, start the
server with --bind_ip 127.0.0.1 to disable this warning.
显然启动的时候要加上 –bind_ip_all 参数