CenntOS 7 安装 MongoDB
安装方式
Docker 安装 MongoDB,请事先安装好 Docker.
安装在服务器上
服务器操作系统为 CentOS Linux release 7.7.1908 (Core)
安装步骤
开放端口
[root@yinyao ~]# firewall-cmd --list-ports 80/tcp 22/tcp [root@yinyao ~]# firewall-cmd --zone=public --add-port=27017/tcp --permanent success [root@yinyao ~]# systemctl restart firewalld.service [root@yinyao ~]# firewall-cmd --list-ports 80/tcp 22/tcp 27017/tcp [root@yinyao ~]#
拉取镜像
[root@yinyao ~]# docker pull mongo Using default tag: latest latest: Pulling from library/mongo 23884877105a: Pull complete bc38caa0f5b9: Pull complete 2910811b6c42: Pull complete 36505266dcc6: Pull complete a4d269900d94: Pull complete 5e2526abb80a: Pull complete d3eece1f39ec: Pull complete 358ed78d3204: Pull complete 1a878b8604ae: Pull complete 978c572f0440: Pull complete 35a600ffcf6a: Pull complete fa9f812cdfe6: Pull complete 7a8109e27110: Pull complete Digest: sha256:be8d903a68997dd63f64479004a7eeb4f0674dde7ab3cbd1145e5658da3a817b Status: Downloaded newer image for mongo:latest docker.io/library/mongo:latest [root@yinyao ~]#
运行 MongoDB
[root@yinyao ~]# docker run --name mongodb -p 27017:27017 -d mongo --auth ce24bf51a7b9ece9522f319417d24400a3d6fe220e5770aa5d44acc3ecf56863 [root@yinyao ~]#
设置管理员与密码
[root@yinyao ~]# docker exec -it mongodb mongo admin MongoDB shell version v4.2.7 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("bf8a1717-11c5-4b46-a6a8-ea1b2f1346e9") } MongoDB server version: 4.2.7 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user > db.createUser({ user: 'root', pwd: 'root', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] }); Successfully added user: { "user" : "root", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } > exit; [root@yinyao ~]#
登录测试
登录时如果不进行用户认证,将无法使用 MongoDB 数据库
登录认证语句:db.auth("root","root");
[root@yinyao ~]# docker exec -it mongodb mongo admin MongoDB shell version v4.2.7 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("a8ac1883-2e9f-45cf-9871-9e4c372b431b") } MongoDB server version: 4.2.7 > show dbs; > db.auth("root","root"); 1 > show dbs; admin 0.000GB config 0.000GB local 0.000GB > exit; [root@yinyao ~]#
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于