环境: centos7.6, mongodb-server v2.6.12
1、增加 epel yum 源
2、安装
yum install -y mongodb-server mongodb3、启动服务
systemctl start mongod4、验证服务开启
mongo5、常用命令
查看数据库
> show dbs admin (empty) local 0.078GB test (empty)选择数据库
> use test switched to db test创建一个集合 person
> db.createCollection('person') { "ok" : 1 } > show collections person system.indexes插入一条数据,save 可以增加修改,如果没有集合还能创建,insert 只能增加
> db.person.save({name: 'json'}) WriteResult({ "nInserted" : 1 }) > db.person.insert({name: 'tom'}) WriteResult({ "nInserted" : 1 })查看集合中的数据
> db.person.find() { "_id" : ObjectId("5f7ed127a0929ccd8805493c"), "name" : "json" } { "_id" : ObjectId("5f7ed188a0929ccd8805493d"), "name" : "tom" }查看集合中的一条数据
> db.person.findOne() { "_id" : ObjectId("5f7ed127a0929ccd8805493c"), "name" : "json" }更改数据
> db.person.update({name:"tom"},{name:"tom2"}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.person.find() { "_id" : ObjectId("5f7ed127a0929ccd8805493c"), "name" : "json" } { "_id" : ObjectId("5f7ed188a0929ccd8805493d"), "name" : "tom2" }如果查询条件和修改字段不同,需要修改器
> db.person.find() { "_id" : ObjectId("5f7ed188a0929ccd8805493d"), "name" : "tom2" } { "_id" : ObjectId("5f7ed127a0929ccd8805493c"), "name" : "json", "age" : 27 }删除集合下某条数据
> db.person.remove({name: "tom2"}) WriteResult({ "nRemoved" : 1 }) > db.person.find() { "_id" : ObjectId("5f7ed127a0929ccd8805493c"), "name" : "json", "age" : 27 }删除集合下所有数据
> db.person.remove({}) WriteResult({ "nRemoved" : 1 }) > db.person.find()删除集合
> db.person.drop() true删除数据库
> db.dropDatabase() { "dropped" : "test", "ok" : 1 } > show dbs admin (empty) local 0.078GB创建数据库
> use test switched to db test > show dbs admin (empty) local 0.078GB test (empty6、修改配置文件 /etc/mongod.conf, 允许远程连接
#bind_ip = 127.0.0.1 bind_ip = 0.0.0.07、对外开放端口(mongodb 默认端口 27017)
8、创建用户、设置账号、密码、权限
> db.createUser({user:"root", pwd:"123123", roles:["root"]}) Successfully added user: { "user" : "root", "roles" : [ "root" ] } > show users { "_id" : "admin.root", "user" : "root", "db" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ] } > db.createUser({user:"admin", pwd:"123123", roles:["readWrite", "dbAdmin"]}) Successfully added user: { "user" : "admin", "roles" : [ "readWrite", "dbAdmin" ] } > show users { "_id" : "test.admin", "user" : "admin", "db" : "test", "roles" : [ { "role" : "readWrite", "db" : "test" }, { "role" : "dbAdmin", "db" : "test" } ] }9、修改配置文件 /etc/mongod.conf, 启用身份验证
auth = true10、重启 mongodb
systemctl restart mongod11、本地登陆
> use admin switched to db admin > db.auth("root","123123") 112、远程连接
[root@k8s01 ~]# mongo 192.168.1.107:27017/admin -u root -p 123123 MongoDB shell version: 2.6.12 connecting to: 192.168.1.107:27017/admin 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 Server has startup warnings: 2020-10-08T17:23:20.396+0800 [initandlisten] 2020-10-08T17:23:20.396+0800 [initandlisten] ** WARNING: Readahead for /var/lib/mongodb is set to 4096KB 2020-10-08T17:23:20.396+0800 [initandlisten] ** We suggest setting it to 256KB (512 sectors) or less 2020-10-08T17:23:20.396+0800 [initandlisten] ** http://dochub.mongodb.org/core/readahead > show dbs admin 0.078GB local 0.078GB参考文章: https://juejin.im/post/6844903556495966215 https://juejin.im/post/6844903828811153421
