下载地址:https://www.mongodb.com/download-center/community
这里下载的版本:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.6.tgz
这里解压到 /home/software/mongodb 目录。
然后创建2个目录用于存放日志和数据。
1 2 | [root@db-mongo ~]# mkdir /home/software/mongodb/log [root@db-mongo ~]# mkdir /home/software/mongodb/data |
# 先用不带身份认证参数启动(–auth)
1 | [root@db-mongo ~]# /home/software/mongodb/bin/mongod --logpath /home/software/mongodb/log/log.log --dbpath /home/software/mongodb/data --maxConns 65535 --bind_ip_all --port 27017 --fork |
# 命令行关闭服务,需要指定db数据目录
1 | [root@db-mongo ~]# /home/software/mongodb/bin/mongod --shutdown --dbpath /home/software/mongodb/data/ |
# mongo控制台关闭
1 2 | [root@db-mongo ~]# /home/software/mongodb/bin/mongo > db.shutdownServer() |
或者用kill 15 发送停止信号。
# 查看库
1 2 3 4 5 | [root@db-mongo ~]# /home/software/mongodb/bin/mongo > show dbs admin 0.000GB config 0.000GB local 0.000GB |
# 切换库,如果库不存在,则会创建。
1 2 | > use test switched to db test |
但此时数据库为空,show dbs看不到。
# 为admin库设置用户名密码,角色为超级管理员
1 2 3 4 5 | > use admin switched to db admin > db.createUser({user:'root',pwd:'111111',roles:['root']}) #只有admin数据库才有root角色 Successfully added user: { "user" : "root", "roles" : [ "root" ] } |
# 修改为带认证参数启动mongodb
1 2 3 4 | [root@db-mongo ~]# /home/software/mongodb/bin/mongod --logpath /home/software/mongodb/log/log.log --dbpath /home/software/mongodb/data --maxConns 65535 --bind_ip_all --port 27017 --fork --auth [root@db-mongo ~]# /home/software/mongodb/bin/mongo > show dbs > |
发现看不到前面3个库了,因为需要认证后才能看。
# 控制台认证admin库的账号密码
1 2 3 4 5 6 7 8 | > use admin switched to db admin > db.auth("root","111111") 1 > show dbs admin 0.000GB config 0.000GB local 0.000GB |
# 新建一个库,并设置密码,注意密码中不要带@艾特符号,mongodb是一个库一个账号。
1 2 3 4 5 6 7 8 | > use rootop > db.createUser({ user:"aaa", pwd:"aaa###bbb", roles:[ {"role":"readWrite","db":"rootop"}, {"role":"dbAdmin","db":"rootop"} ]}) |
# 插入数据
1 2 | > db.rootop.insert({"user":"aaa"}) WriteResult({ "nInserted" : 1 }) |
# 启动关闭脚本保存为 mon.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/bin/bash need_pass="--auth" if [ "$2" == "false" ];then need_pass="" fi case $1 in start) /home/software/mongodb/bin/mongod --logpath /home/software/mongodb/log/log.log --dbpath /home/software/mongodb/data --maxConns 65535 --bind_ip_all --port 27017 --journal --fork $need_pass --wiredTigerCacheSizeGB=1 ;; stop) /home/software/mongodb/bin/mongod --shutdown --dbpath /home/software/mongodb/data/ ;; *) echo sh mon.sh start [false]/stop ;; esac |
在测试环境发现mongo内存占用挺大,限制下内存。
# 限制内存参数,加到启动参数里。
比如限制为1G
1 | --wiredTigerCacheSizeGB=1 |
# mongodb官方客户端
https://www.mongodb.com/download-center/compass
# 导出指定库,注意用库所对应的账密
1 | [root@db-mongo ~]# /home/software/mongodb/bin/mongodump -h 127.0.0.1 --port 27017 -u root -p 123456 -d xxx -o /root/backup/xxx # -o 保存备份的目录 |
# 导入指定库,注意用库所对应的账密
1 | [root@db-mongo ~]# /home/software/mongodb/bin/mongorestore -h 127.0.0.1 --port 27017 -u root -p 123456 -d xxx --drop /root/backup/xxx |
# 备份脚本,注意备份哪个库,就用哪个库的账号
1 2 3 4 5 6 7 8 9 10 | #!/bin/bash mongodump=/home/software/mongodb/bin/mongodump host=127.0.0.1 port=27017 user=root pass=123456 db=xxx path=/home/mongobak dir_name=$(date +%Y-%m-%d-%H-%M-%S) $mongodump -h $host --port $port -u $user -p $pass -d $db -o $path/$db-$dir_name |