安装mongodb
下载安装mongodb3.4.x
cd ~ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.2.tgz tar -zxvf mongodb-linux-x86_64-3.4.2.tgz cp -r /root/mongodb-linux-x86_64-3.4.2 /usr/local/mongodb
加入环境变量
vim /etc/profile export PATH=$PATH:/usr/local/mongodb/bin ESC :wq source /etc/profile
创建数据目录
mkdir /data/ mkdir /data/mongodb/ mkdir /data/mongodb/logs/ mkdir /data/mongodb/data/
修改配置文件
vim /usr/local/mongodb/mongodb.conf fork = true port = 27017 logappend=true nohttpinterface = false dbpath=/data/mongodb/data/ logpath=/data/mongodb/logs/mongodb.log pidfilepath=/data/mongodb/logs/mongodb.pid ESC :wq
启动关闭mongodb
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf --shutdown
开机启动mongodb
cd /etc/systemd/system vim mongodb.service [Unit] Description=Start mongodb on boot. After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/data/mongodb/logs/mongodb.pid ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target ESC :wq systemctl daemon-reload systemctl enable mongodb systemctl start mongodb
Mongodb集群(cluster)
修改mongodb.conf
vim /usr/local/mongodb/mongodb.conf fork=true port=27017 logappend=true # 先查看本机IP($ ip addr),再绑定内网IP bind_ip=192.168.1.150 nounixsocket=false nohttpinterface=true dbpath=/data/mongodb/data/ unixSocketPrefix=/data/mongodb/data/ logpath=/data/mongodb/logs/mongodb.log pidfilepath=/data/mongodb/logs/mongodb.pid replSet=Rs1/192.168.1.150:27017 ESC :wq
启动mongodb
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
初始化mongodb,启动副本集
# 查看副本集状态 rs.status() # 1. 启动新副本集 rs.initiate() # 2. 配置mongodb服务节点 config_rs1={ _id:'Rs1',members:[ {_id:0,host:'192.168.1.149:27017',priority:1}, {_id:1,host:'192.168.1.150:27017',priority:1} ] } # 显示当前簇 rs.isMaster()
关闭mongodb
/usr/local/mongodb-3.4.2/bin/mongod --shutdown --config /usr/local/mongodb-3.4.2/mongodb.conf
Mongodb常用命令
# 显示所有数据库 show dbs; # 选中数据库 use db_name; # 删除数据库 db.dropDatabase(); # 显示所有集合 show collections; # 创建新的集合 db.createCollection(COLLECTION_NAME, option); # 删除一个集合(COLLECTION_NAME) db.COLLECTION_NAME.drop(); # 向集合插入数据 db.COLLECTION_NAME.insert({name:"dog",age:2}) # 删除数据 db.COLLECTION_NAME.remove({}) # 退出Mongodb quit;
- 领支付宝红包支持作者