Centos下安装MongoDB4.2.3
1、安装MongoDB
yum install mongodb-org-server-4.2.3-1.el7.x86_64.rpm yum install mongodb-org-tools-4.2.3-1.el7.x86_64.rpm mongodb-org-shell-4.2.3-1.el7.x86_64.rpm
2、根目录下创建一个mongodb的文件夹,进入后再分别创建data和log文件夹,进入log文件夹创建mongo.log的文件,并且修改权限
[ /]# cd mongodb/ [ mongodb]# ll total 0 [ mongodb]# mkdir data [ mongodb]# mkdir log [ mongodb]# ll total 0 drwxr-xr-x. 2 root root 6 Mar 11 16:36 data drwxr-xr-x. 2 root root 6 Mar 11 16:36 log [ mongodb]# cd log/ [ log]# touch mongo.log [ log]# ll total 0 -rw-r--r--. 1 root root 0 Mar 11 16:38 mongo.log [ /]# chown -R mongod:mongod /mongodb
3、修改配置 文件
[ etc]# cat mongod.conf # mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: /mongodb/log/mongo.log //日志路径 # Where and how to store data. storage: dbPath: /mongodb/data //数据库文件存放路径 journal: enabled: true # engine: # wiredTiger: # how the process runs processManagement: fork: true # fork and run in background pidFilePath: /mongodb/data/mongod.pid # location of pidfile //修改为新的路径 timeZoneInfo: /usr/share/zoneinfo # network interfaces net: port: 28017 //端口 bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting. security: authorization: enabled //开启验证 #operationProfiling: #replication: #sharding: ## Enterprise-Only Options #auditLog: #snmp:
修改/usr/lib/systemd/system/mongod.service文件
[ /]# cat /usr/lib/systemd/system/mongod.service [Unit] Description=MongoDB Database Server Documentation=https://docs.mongodb.org/manual After=network.target [Service] User=mongod Group=mongod Environment="OPTIONS=-f /etc/mongod.conf" EnvironmentFile=-/etc/sysconfig/mongod ExecStart=/usr/bin/mongod $OPTIONS ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb PermissionsStartOnly=true PIDFile=/mongodb/data/mongod.pid //路径修改为和mongod.conf一样 Type=forking # file size LimitFSIZE=infinity # cpu time LimitCPU=infinity # virtual memory size LimitAS=infinity # open files LimitNOFILE=64000 # processes/threads LimitNPROC=64000 # locked memory LimitMEMLOCK=infinity # total threads (user+kernel) TasksMax=infinity TasksAccounting=false # Recommended limits for for mongod as specified in # http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings [Install] WantedBy=multi-user.target
4、开启端口
firewall-cmd --zone=public --add-port=28017/tcp --permanent 命令含义: –zone #作用域 –add-port=80/tcp #添加端口,格式为:端口/通讯协议 –permanent #永久生效,没有此参数重启后失效 重启防火墙 firewall-cmd --reload #重启firewall systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动 firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running) 查看已经开放的端口: firewall-cmd --list-ports
5、启动服务:
systemctl daemon-reload systemctl start mongod
6、登陆mongodb
[ /]# mongo --port 28017 MongoDB shell version v4.2.3 connecting to: mongodb://127.0.0.1:28017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("ced6be6c-0a7f-48c5-86d9-cf19374eca62") } MongoDB server version: 4.2.3 > use admin switched to db admin > db.createUser({user:"admin", pwd:"admin", roles:[{role:"root", db:"admin"}]}) //描述:使用密码admin创建用户管理员,并将权限/角色作为root用户,数据库为admin。 Successfully added user: { "user" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ] } >
7、开启验证
打开/lib/systemd/system/mongod.service文件并在’ExecStart’中,添加新选项’–auth’。
ExecStart=/usr/bin/mongod –auth –config /etc/mongod.conf
重新加载systemd服务:
[ /]# systemctl daemon-reload [ /]# systemctl stop mongod [ /]# systemctl start mongod [ /]# systemctl status mongod â?? mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2020-03-12 11:06:15 CST; 5s ago Docs: https://docs.mongodb.org/manual Process: 9703 ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf (code=exited, status=0/SUCCESS) Process: 9700 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 9695 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 9692 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS) Main PID: 9705 (mongod) Tasks: 32 CGroup: /system.slice/mongod.service â??â??9705 /usr/bin/mongod --auth --config /etc/mongod.conf
8、连接数据库
[roo /]# mongo --port 28017 -u admin -p admin --authenticationDatabase admin MongoDB shell version v4.2.3 connecting to: mongodb://127.0.0.1:28017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("035c66ec-394c-4bbe-aecf-3af87710287e") } MongoDB server version: 4.2.3 Server has startup warnings: 2020-03-12T11:06:15.952+0800 I CONTROL [initandlisten] 2020-03-12T11:06:15.952+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘. 2020-03-12T11:06:15.952+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘ 2020-03-12T11:06:15.953+0800 I CONTROL [initandlisten] 2020-03-12T11:06:15.953+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘. 2020-03-12T11:06:15.953+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘ 2020-03-12T11:06:15.953+0800 I CONTROL [initandlisten] --- Enable MongoDB‘s free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- >
9、添加一新用户并开启登录权限
> db.createUser({user:"test", pwd:"test", roles:[{role:"readWrite", db:"test"}]}) Successfully added user: { "user" : "test", "roles" : [ { "role" : "readWrite", "db" : "test" } ] } //创建test账号并创建test数据库,读写权限
10、将数据库导入
mongorestore --port 28017 -u test -p test -d test /backup/test
11、数据库导出:
mongodump --port 28017 -u test -p test -d test
12、删除集合:每次restore数据库的时候先删除之前的集合
> use test switched to db test > show collections LotAttribute_A011 LotAttribute_A011_bak LotAttribute_A011_copy LotAttribute_A012 LotAttribute_A012_bak LotAttribute_Change_A011 LotDetectAttribute_A011 MESTemplate_A012 ProductionProperty ProductionPropertyCategory ProductionProperty_Test > db.LotAttribute_A011.drop() true
相关推荐
大秦铁骑 2020-08-19
thatway 2020-08-19
lovecodeblog 2020-08-19
codetyper 2020-08-16
MongoDB数据库 2020-08-16
csuzxm000 2020-08-02
flyDeDog 2020-06-14
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08