centos 6.8 部署mongodb3.6.2主从
环境:
主:172.17.165.245
从:172.17.165.230
mongodb版本:mongodb3.6.2
下载程序
root@iZ2zejbqrpwrmr4mjy0u9fZ opt]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.6.2.tgz [root@iZ2zejbqrpwrmr4mjy0u9fZ opt]# tar -zxvf mongodb-linux-x86_64-rhel62-3.6.2.tgz -C /usr/local/ [root@iZ2zejbqrpwrmr4mjy0u9fZ local]# mv mongodb-linux-x86_64-rhel62-3.6.2/ mongodb [root@iZ2zejbqrpwrmr4mjy0u9fZ mongodb]# mkdir -p /data/mongodb [root@iZ2zejbqrpwrmr4mjy0u9fZ mongodb]# mkdir logs [root@iZ2zejbqrpwrmr4mjy0u9fZ mongodb]# cd logs/ [root@iZ2zejbqrpwrmr4mjy0u9fZ logs]# touch mongodb.log [root@iZ2zejbqrpwrmr4mjy0u9fZ logs]# mkdir -p /var/run/mongodb
master [root@iZ2zegc312qxcxs61cotxlZ opt]# cat /usr/local/mongodb/mongdb.conf port = 27017 logpath = /usr/local/mongodb/logs/mongodb.log pidfilepath=/var/run/mongodb/mongodb.pid dbpath=/data/mongodb master = true #确定我是主服务器 source = 172.17.165.230 fork = true logappend = true #auth = true slave [root@iZ2zejbqrpwrmr4mjy0u9fZ ~]# cat /usr/local/mongodb/mongodb.conf port = 27017 #端口号 logpath = /usr/local/mongodb/logs/mongodb.log #日志位置 pidfilepath=/var/run/mongodb/mongodb.pid #PID 位置 dbpath=/data/mongodb #数据存放位置 slave = true #模式 确定我是从服务器 source = 172.17.165.245 #规定从属于哪个ip 注意:ip是主服务器的 fork = true #设置后台运行 logappend = true #日志输出格式 #auth = true #开启认证
设置环境变量
[root@iZ2zejbqrpwrmr4mjy0u9fZ ~]# vim /etc/profile export PATH=/usr/local/mongodb/bin:$PATH
启动程序
[root@iZ2zejbqrpwrmr4mjy0u9fZ ~]# mongod --config /usr/local/mongodb/mongodb.conf about to fork child process,waiting until server is ready for connections. forked process: 32287 child process started successfully,parent exiting 说明程序已经正常启动
常用语句:
use admin 创建关于用户db.createUser({user:"admin",pwd:"admin",roles:[{role:"root",db:"admin"}]}) > rs.slaveOk()
主库插入语句
mongo 172.17.165.245:27017 创建新库 插入数据 > use new10 switched to db new10 > db.new10.save({"name":22200}) WriteResult({ "nInserted" : 1 }) > db.new10.find() { "_id" : ObjectId("5a7d7544ab1efad7a5581285"),"name" : 22200 }
从库进行查看
[root@iZ2zejbqrpwrmr4mjy0u9fZ mongodb]# mongo > use admin switched to db admin > db.auth("admin","admin") 1 > > rs.slaveOk() > show dbs admin 0.000GB config 0.000GB local 0.000GB master_slave 0.000GB new1 0.000GB new10 0.000GB teot 0.000GB test 0.000GB > switched to db new10 > db.new10.find() { "_id" : ObjectId("5a7d7544ab1efad7a5581285"),"name" : 22200 }
从库上面已经查看了主库上面的数据 说明主从已经搭建完毕