[root@lijia1 mongodb3222]# mkdir -p /var/lib/mongo
[root@lijia1 mongodb3222]# mkdir -p /var/log/mongodb
vi /etc/profile export MONGODB_HOME=/opt/bigdata/mongodb3222 export PATH= P A T H : PATH: PATH:MONGODB_HOME/bin
[root@lijia1 mongodb3222]# mongod --dbpath /var/lib/mongo --logpath /var/log/mongod.log --fork
[root@lijia1 mongodb3222]# mongo
默认是test数据库 查看当前数据库: db
切换数据库 use kb07 use admin
查看数据库中的表 show tables
在表中插入数据 db.runoob.insert({x:10})
删除runoob表 db.runoob.drop()
查看当前库中的用户 show users
给数据库kb07创建用户
db.createUser({user:“gree”,pwd:“gree”,roles:[{role:“readWrite”, db:“kb07”}]}) Successfully added user: { “user” : “gree”, “roles” : [ { “role” : “readWrite”, “db” : “kb07” } ] }
创建用户
use admin switched to db admin
db.createUser({user:“dba”,pwd:“dba”, roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ]})
db.auth(‘dba’,‘dba’) 1
show users { “_id” : “admin.dba”, “user” : “dba”, “db” : “admin”, “roles” : [ { “role” : “userAdminAnyDatabase”, “db” : “admin” } ] }
show dbs admin 0.078GB kb05 0.078GB local 0.078GB test 0.078GB
db.createUser({user:“gree”,pwd:“gree”,roles:[{role:“readWrite”,db:“kb05”}]}) Successfully added user: { “user” : “gree”, “roles” : [ { “role” : “readWrite”, “db” : “kb05” } ] }
2+2 4 db test show dbs local 0.078GB use admin switched to db admin db.createUser( … { … user:“dba”, … pwd:“dba”, … roles:[ { role: “userAdminAnyDatabase”, db: “admin” }] … } … ) Successfully added user: { “user” : “dba”, “roles” : [ { “role” : “userAdminAnyDatabase”, “db” : “admin” } ] } ^C bye [root@lijia1 bin]# mongo MongoDB shell version: 3.0.6 connecting to: test show dbs 2020-06-10T16:12:29.689+0800 E QUERY Error: listDatabases failed:{ “ok” : 0, “errmsg” : “not authorized on admin to execute command { listDatabases: 1.0 }”, “code” : 13 } at Error () at Mongo.getDBs (src/mongo/shell/mongo.js:47:15) at shellHelper.show (src/mongo/shell/utils.js:630:33) at shellHelper (src/mongo/shell/utils.js:524:36) at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47 use admin switched to db admin db.auth(‘dba’,‘dba’) 1 ues test 2020-06-10T16:13:00.799+0800 E QUERY SyntaxError: Unexpected identifier use test switched to db test db.createUser( … { … user:“test”, … pwd:“test”, … roles:[{role:“readWrite”, db:“test”}] … } … ) Successfully added user: { “user” : “test”, “roles” : [ { “role” : “readWrite”, “db” : “test” } ] } ^C bye [root@lijia1 bin]# mongo MongoDB shell version: 3.0.6 connecting to: test use test switched to db test db.auth(‘test’,‘test’) 1 db test db test show tables; students system.indexes
JAVA代码 public class MongoTest { public static void main(String[] args) { try { ServerAddress serverAddress = new ServerAddress(“192.168.153.141”, 27017); List addrs = new ArrayList<>(); addrs.add(serverAddress);
MongoCredential credential = MongoCredential.createScramSha1Credential("test", "test","test".toCharArray()); List<MongoCredential> credentials = new ArrayList<>(); credentials.add(credential); MongoClient mongoClient = new MongoClient(addrs, credentials); final MongoDatabase test = mongoClient.getDatabase("test"); System.out.println(test); test.createCollection("students"); System.out.println("successfull"); } catch (Exception e) { e.printStackTrace(); } }}