mongo笔记记录
一、mongo.conf配置文件
logpath=/usr/local/mongo2/log/mongodb.log
logappend=true
dbpath=/usr/local/mongo2/data/db
port = 27018
journal=true
quiet=true
fork=true
bind_ip=0.0.0.0
二、mongo安装启动流程
#将文件上传到云服务器
[root@LinCheng ~]# ls
install.sh mongodb-linux-x86_64-rhel70-4.4.2.tgz
#解压文件
[root@LinCheng ~]# tar -zxvf mongodb-linux-x86_64-rhel70-4.4.2.tgz
mongodb-linux-x86_64-rhel70-4.4.2/LICENSE-Community.txt
mongodb-linux-x86_64-rhel70-4.4.2/MPL-2
mongodb-linux-x86_64-rhel70-4.4.2/README
mongodb-linux-x86_64-rhel70-4.4.2/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-4.4.2/bin/install_compass
mongodb-linux-x86_64-rhel70-4.4.2/bin/mongo
mongodb-linux-x86_64-rhel70-4.4.2/bin/mongod
mongodb-linux-x86_64-rhel70-4.4.2/bin/mongos
#查看解压后的文件
[root@LinCheng ~]# ls
install.sh mongodb-linux-x86_64-rhel70-4.4.2 mongodb-linux-x86_64-rhel70-4.4.2.tgz
# 移动该文件到/usr/local/mongo2 且重命名
[root@LinCheng ~]# mv mongodb-linux-x86_64-rhel70-4.4.2 /usr/local/mongo2
#此时查看根目录没有解压后的文件
[root@LinCheng ~]# ls
install.sh mongodb-linux-x86_64-rhel70-4.4.2.tgz
# 进入/usr/local后查看当前目录文件
[root@LinCheng ~]# cd /usr/local
[root@LinCheng local]# ls
aegis curl games lib libexec man mongo2 openssl sbin src
bin etc include lib64 libiconv mongo nghttp2 openssl111 share
[root@LinCheng local]# cd mongo2
[root@LinCheng mongo2]# ls
bin LICENSE-Community.txt MPL-2 README THIRD-PARTY-NOTICES
#新建目录存放mongo的数据文件和日志文件及配置文件
[root@LinCheng mongo2]# mkdir -p data/db
[root@LinCheng mongo2]# mkdir log conf
[root@LinCheng mongo2]# ls
bin conf data LICENSE-Community.txt log MPL-2 README THIRD-PARTY-NOTICES
# 进入conf文件编辑配置mongo配置文件,配置文件上方已给出
[root@LinCheng mongo2]# cd conf
[root@LinCheng conf]# vi mongo.conf
# 进入到bin目录下启动mongo ,可配置环境变量在哪都可启动mongo
[root@LinCheng conf]# cd ../bin
[root@LinCheng bin]# ls
install_compass mongo mongod mongos
# 指定配置文件启动mongo
[root@LinCheng bin]# ./mongod -f /usr/local/mongo2/conf/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 30354
child process started successfully, parent exiting
[root@LinCheng bin]#
# 连接mongo
[root@LinCheng bin]# ./mongo
MongoDB shell version v4.4.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8f413c94-9735-499c-9b3b-cff651668a7b") }
MongoDB server version: 4.0.21
WARNING: shell and server versions do not match
---
The server generated these startup warnings when booting:
2020-11-26T13:49:06.871+0800 I STORAGE [initandlisten]
2020-11-26T13:49:06.871+0800 I STORAGE [initandlisten] ** WARNING: You have explicitly specified 'MMAPV1' storage engine in your
2020-11-26T13:49:06.871+0800 I STORAGE [initandlisten] ** config file or as a command line option. Support for the MMAPV1
2020-11-26T13:49:06.871+0800 I STORAGE [initandlisten] ** storage engine has been deprecated and will be removed in
2020-11-26T13:49:06.871+0800 I STORAGE [initandlisten]
2020-11-26T13:49:06.885+0800 I STORAGE [initandlisten]
2020-11-26T13:49:06.885+0800 I STORAGE [initandlisten] ** WARNING: Readahead for /usr/local/mongo/data/db is set to 4096KB
---
# 此时已经进入mongo数据库
> db
test
> show dbs
admin 0.078GB
config 0.078GB
local 0.078GB
#切换用户 且关闭mongo服务
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
>
三、踩坑及解决方法
报错记录
在启动时报这个错误,可能是数据不好的关闭mongo服务导致
about to fork child process, waiting until server is ready for connections.
forked process: 30541
ERROR: child process failed, exited with 14
[root@LinCheng bin]# ls
install_compass mongo mongod mongos
#在启动时报这个错误,可能是数据不好的关闭mongo服务导致
[root@LinCheng bin]# ./mongod -f /usr/local/mongo2/conf/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 30541
ERROR: child process failed, exited with 14
#解决方法
第一、先删除data/db目录下的mongod/lock,根据自己的路径进行删除
rm -f /usr/local/mongo2/data/db/mongod.lock
第二、修复data/db目录下的数据
/usr/local/mongo2/bin/mongod --repair --dbpath=/usr/local/mongo2/data/db/
接下来就能启动运行了
./mongod -f /usr/local/mongo2/conf/mongo.conf
为了少遇到上面启动报错,建议使用一下关闭mongo服务的方法,不要直接kill杀死进程,这样容易导致数据损失 造成启动不了
#切换用户 且关闭mongo服务
# 在关闭之前查看mongo后台进程
[root@LinCheng bin]# ps -ef |grep mongod
root 30598 1 0 10:31 ? 00:00:03 ./mongod -f /usr/local/mongo2/conf/mongo.conf
root 30661 30170 0 10:39 pts/0 00:00:00 grep --color=auto mongod
[root@LinCheng bin]# ./mongo
客户端也可以采用另一种方法登录
//客户端登录服务,注意,这里通过localhost登录,如果需要远程登录,必须先登录认证才行。
[root@LinCheng bin]# mongo --port 27017
> use admin
switched to db admin
# 关闭mongo服务
> db.shutdownServer()
server should be down...
# 在关闭服务后再来查看后台进程,发现mongod服务关闭了
[root@LinCheng bin]# ps -ef |grep mongod
root 30675 30170 0 10:40 pts/0 00:00:00 grep --color=auto mongod
[root@LinCheng bin]#