2020年04月19日 10:10 阅读量:1341
1、下载 mongodb 3.2.3
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.3.tgz
2、解压
tar -zxvf mongodb-linux-x86_64-3.2.3.tgz
3、指定安装目录(将解压文件移动到/usr/local/mongodb
文件夹中)
mv mongodb-linux-x86_64-3.2.3 /usr/local/mongodb
4、在mongodb文件夹中创建数据文件目录和log文件目录
cd /usr/local/mongodb
groupadd mongodb
useradd -s /sbin/nologin -g mongodb -M mongodb
mkdir data logs run
chown -R mongodb:mongodb data logs run
5、在mongodb/bin目录下创建配置文件mongodb.conf
cd bin/
vi mongodb.conf
配置信息如下:
dbpath = /usr/local/mongodb/data #数据文件存放目录
logpath = /usr/local/mongodb/logs/mongodb.log #日志文件存放目录
pidfilepath = /usr/local/mongodb/run/mongodb.pid
port = 27017 #端口
fork = true #以守护程序的方式启用,即在后台运行
nohttpinterface = true
logappend=true
auth=true
参数说明:
dbpath:数据库文件路径
logpath:日志文件路径
logappend:是否追加日志
port:端口
fork:是否以后台进程启动
auth:是否启动授权认证
nohttpinterface:是否支持HTTP形式访问
6、启动mongodb程序(使用配置文件mongodb.conf
定义的参数启动) --忽略这步
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/bin/mongodb.conf
7、开启mongodb服务随系统自启动,编写自定义服务
(1).在/lib/systemd/system/目录下新建mongodb.service文件,
cd /lib/systemd/system/
vim mongodb.service
内容如下:
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
User=mongodb
Group=mongodb
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/bin/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/bin/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
(2).设置权限
chmod 754 mongodb.service
(3).启动关闭服务,设置开机启动
启动并加入开机启动
systemctl start mongodb.service
开机启动
systemctl enable mongodb.service
关闭服务
systemctl stop mongodb.service
配置mongodb为环境变量,方便直接在shell中操作
vim /etc/profile
在/etc/profile文件末尾添加一行:
export PATH=/usr/local/mongodb/bin:$PATH
让其生效:
source /etc/profile
8、添加超级管理员用户
/usr/local/mongodb/bin/mongo
./mongo
use admin
db.createUser(
{
user: "root",
pwd: "123456",
roles: [
{ role: "userAdminAnyDatabase", db: "admin" }
]
}
)
登陆管理员
use admin
db.auth("root","123456");
use 要创建的新数据库名称
use WebChatAnti
db.createUser({user:"WebChatAnti",pwd:"123456",roles:[{role:"dbOwner",db:"WebChatAnti"}]})
db.auth("WebChatAnti","123456");
查所有表
show collections
删除表
db.users.drop()
查users表
db.users.find();
更新users表
db.users.update({"_id": ObjectId("5e9a649cc85d706d4554314a")},{$set:{"username":"zhangsan"}});
查看当前的引擎状态:
db.serverStatus()
./mongod --dbpath=/usr/local/mongodb/data/wiredtiger --logpath=/usr/local/mongodb/data/wiredtiger/log
./mongod --dbpath=/usr/local/mongodb/data/mmapv1 --logpath=/usr/local/mongodb/data/mmapv1/log
./mongod --storageEngine MMAPv1
./mongod --storageEngine wiredTiger