A, mongodb installation
1, environment preparation
System version CentOS 6.9
Software version mongondb-4.0.1
2, installation mongondb
# cd /usr/local/src
Wget # https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-4.0.1.tgz download
# tar zxvf mongodb-linux-x86_64-rhel62-4.0.1.tgz -C /usr/local/
# cd /usr/local/
# ln -s mongodb-linux-x86_64-rhel62-4.0.1 mongodb
# Mkdir -p / usr / local / dbdata / mongodb create a data storage directory
(# Mkdir -p / usr / local / dbdata / mongodb / {mongodb27017, mongodb27018}) for the two examples of preparation
# Mkdir -p / usr / local / dbconf / mongodb create profile directory
# Mkdir -p / usr / local / dblog / mongodb create a log file directory
# Vim /usr/local/dbconf/mongodb/mongodb27017.conf initiative to create a configuration file ( the installation package templates did not )
port = 27017 # default server port number dbpath = / usr / local / dbdata / mongodb / mongodb27017 # data storage directory logpath = / usr / local / dblog / mongodb / mongodb27017.log # log file logappend = true # Use additional way to write the log fork = true # running in the background maxConns = 5000
# / Usr / local / mongodb / bin / mongod -f /usr/local/dbconf/mongodb/mongodb27017.conf start example
# / Usr / local / mongodb / bin / mongod -f /usr/local/dbconf/mongodb/mongodb27017.conf --shutdown stop the instance
# / Usr / local / mongodb / bin / mongo --host 127.0.0.1:27017 connection instance
Second, the basic operation
Basic Operation
show dbs; # All database show collections; # Display Set current database (similar to the relational database tables) Show Users; # Check current user information database use < DB name > ; # switching database with mysql as DB; or db.getName (); # View current database db.help (); # display database operations command, there are a lot of command db.foo.help (); # display set operations command, also has a lot of command, foo refers to the database is a collection called foo present, not in the true sense command db.foo.find (); # data for the current database foo set of lookup (because there is no condition, will list all of the data) db. foo.find ({a: 1 }); # lookup database foo the current set of data that has a property called a, and a value of 1
Create a test database example:
> Use test; # create the database, if the test inventory is cut into test library, it does not exist is to create a test database, but behind the need to create a table like immediately, otherwise it will be automatically cleans Switched to db test > db; test > Show dbs; # check database ADMIN 0 .000GB local 0 .000GB > db.test. INSERT ({ "the _id": " 520. ", "name": "Xiaoming"}) # Create a table WriteResult ({ "nInserted": . 1 } ) > db.createUser ({ User : "Xiaoming", pwd: " 123456 ", Roles: [ {Role: "useradmin", DB: "Test"} ] }) # Create a user Added Successfully User : { " User ": "Xiaoming", "Roles": [ { "Role": "useradmin", "DB": "Test" } ] } db.removeUser ( "the userName"); # delete user show users; # display all current users
db.dropDatabase (); # delete the current use of the database
> show dbs; admin 0.000GB local 0.000GB test 0.000GB test_1 0.000GB > db; test_1 > db.dropDatabase(); { "dropped" : "test_1", "ok" : 1 } > show dbs; admin 0.000GB local 0.000GB test 0.000GB
db.stats (); # shows the current db state
> db.stats(); { "db" : "test_1", "collections" : 0, "views" : 0, "objects" : 0, "avgObjSize" : 0, "dataSize" : 0, "storageSize" : 0, "numExtents" : 0, "indexes" : 0, "indexSize" : 0, "fileSize" : 0, "ok" : 1 }
db.version (); # current db version
> db.version(); 3.4.10
db.getMongo (); # to view the current db link address of the machine
> db.getMongo(); connection to 172.16.40.205:27017
Open Remote Access
Edit the configuration file: vi / etc / mongod.conf bindIp: 172.16 . 40.205 # IP address of the server where the database stored resume database! > . / Mongo 172.16 . 40.205 : 27017 / ADMIN - U the User - the p-password