01.centos7安装MongoDB
1.1 安装MongoDB
[root@k8s-node1 ~]# vim /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
yum安装
[root@k8s-node1 ~]# sudo yum install -y mongodb-org
1.2 配置MongoDB
- 配置
mongod.conf
允许远程连接
[root@k8s-node1 ~]# vim /etc/mongod.conf
# network interfaces
net:
port: 27017
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.
启动mongo
[root@k8s-node1 ~]# sudo service mongod start
1.3 创建管理员
- 创建管理员用户
[root@k8s-node1 ~]# mongo
> use admin
> db.createUser(
{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
- 启用权限管理
[root@k8s-node1 ~]# vim /etc/mongod.conf
#security
security:
authorization: enabled
- 重启mongodb
[root@k8s-node1 ~]# sudo service mongod restart
- 登录MongoDB
[root@k8s-node1 ~]# mongo --host 127.0.0.1 -u "myUserAdmin" --authenticationDatabase "admin" -p'abc123'
1.4 测试可用性
[root@k8s-node1 ~]# mongo --host 127.0.0.1 -u "myUserAdmin" --authenticationDatabase "admin" -p'abc123'
> show dbs # 查看所有db
> use mydemo # 使用我们的数据库(如果没有这个数据库会自动创建)
> db.test1.insert({id:1,"name":"Tom"}) # 在test1表中插入一条数据(如果没有这个表会自动创建)
__END__