In the past, MySQL databases were used to write a large pile of SQL statements all day long. It took several days to remember these SQL keywords, and all the SQL keywords were written. When MongoDB was exposed, I found that I don't need to write SQL statements. I instantly felt that I was so tall that I was eager to learn how to use it. 1. MongoDB is a powerful, flexible, and scalable data storage device.
In the past, MySQL databases were used to write a large pile of SQL statements all day long. It took several days to remember these SQL keywords, and all the SQL keywords were written. When MongoDB was exposed, I found that I don't need to write SQL statements. I instantly felt that I was so tall that I was eager to learn how to use it. 1. MongoDB is a powerful, flexible, and scalable data storage device.
In the past, MySQL databases were used to write a large pile of SQL statements all day long. It took several days to remember these SQL keywords, and all the SQL keywords were written. When MongoDB was exposed, I found that I don't need to write SQL statements. I instantly felt that I was so tall that I was eager to learn how to use it.
1. MongoDB Introduction
MongoDB is a powerful, flexible, and Scalable Data Storage Method. It extends many useful functions of relational databases, such as secondary indexes, range queries, and sorting. MongoDB has rich functions, such as built-in support for MapReduce-type aggregation and geospatial indexing. There are many other features...
For entry-level people, all of the above are cloud float. I don't know its capacity, horizontal scaling, or its speed, these are the issues that will be concerned when we have reached a certain level. I simply think its advantage is that it is very easy to use and provides a series of Apis without the need to write SQL statements. What is the difference between RDS and RDs? What are the advantages and disadvantages of RDS and what needs to be improved.
2. MongoDB Installation
Mongodb is available on the official website. Based on the system windows, linux, or other 32-bit or 64-bit downloads, unzip the package and install it. I installed it on Linux. I first installed it on my own virtual machine using yum as the root user. Later I used secureCRT to connect to other servers and install it on my own users, in this way, you can play with your own mongodb and won't get rid of the server, but you can't use yum. The source code installation seems to have a lot of software dependencies, so I downloaded a linux version on the official website, and then uploaded it to the server through SFTP for decompression.
3. MongoDB Management
After decompression, enter the bin directory, which contains some executable files, such as mongo, mongod, mongodump, and mongoexport. Let's talk about their functions later.
1). Start and Stop MongoDB
Run mongod to start the MongoDB server. mongod has many options for starting configuration. You can view them through mongod -- help. There are some main options:
-- Dbpath: Specifies the data directory. The default value is/data/db /. Each mongod process requires an independent directory. When mongod is started, the mongod. lock file is created in the data directory to prevent other mongod processes from using the data directory.
-- Port: Specifies the port listened to by the server. The default value is 27017.
-- Fork: Run MongoDB as a daemon.
-- Logpath: Specifies the log output path. If this parameter is not specified, it is output on the terminal. The original logs are overwritten at each startup. If you do not want to overwrite the logs, use the -- logappend option.
-- Config: Specify the configuration file to load various options not specified by the command line. We can talk about the options we need to use in a file, and then use this option to specify this file, so you don't have to write it every time you start mongod.
For example, my configuration file mongodb. conf has the following Configuration:
Port = 9352 # fork = true # comment # logpath = mongodb. logdbpath = data/db
Run mongod to start the MongoDB server and specify the configuration file:
[tp0352@server0 bin]$ ./mongod --config mongodb.conf 2014-06-05T17:11:13.118+0800 [initandlisten] MongoDB starting : pid=18077 port=9352 dbpath=data/db 64-bit host=server0.1692014-06-05T17:11:13.119+0800 [initandlisten] db version v2.6.12014-06-05T17:11:13.119+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e82014-06-05T17:11:13.119+0800 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_492014-06-05T17:11:13.119+0800 [initandlisten] allocator: tcmalloc2014-06-05T17:11:13.119+0800 [initandlisten] options: { config: "mongodb.conf", net: { port: 9352 }, storage: { dbPath: "data/db" } }2014-06-05T17:11:13.246+0800 [initandlisten] journal dir=data/db/journal2014-06-05T17:11:13.246+0800 [initandlisten] recover : no journal files present, no recovery needed2014-06-05T17:11:15.333+0800 [initandlisten] preallocateIsFaster=true 20.742014-06-05T17:11:17.692+0800 [initandlisten] preallocateIsFaster=true 13.722014-06-05T17:11:20.858+0800 [initandlisten] preallocateIsFaster=true 23.862014-06-05T17:11:20.858+0800 [initandlisten] preallocateIsFaster check took 7.612 secs2014-06-05T17:11:20.858+0800 [initandlisten] preallocating a journal file data/db/journal/prealloc.02014-06-05T17:11:23.121+0800 [initandlisten] File Preallocator Progress: 492830720/1073741824 45%2014-06-05T17:11:26.424+0800 [initandlisten] File Preallocator Progress: 681574400/1073741824 63%2014-06-05T17:11:29.004+0800 [initandlisten] File Preallocator Progress: 964689920/1073741824 89%2014-06-05T17:11:34.558+0800 [initandlisten] preallocating a journal file data/db/journal/prealloc.12014-06-05T17:11:37.089+0800 [initandlisten] File Preallocator Progress: 524288000/1073741824 48%2014-06-05T17:11:40.090+0800 [initandlisten] File Preallocator Progress: 807403520/1073741824 75%2014-06-05T17:11:43.063+0800 [initandlisten] File Preallocator Progress: 1017118720/1073741824 94%2014-06-05T17:11:48.020+0800 [initandlisten] preallocating a journal file data/db/journal/prealloc.22014-06-05T17:11:51.040+0800 [initandlisten] File Preallocator Progress: 576716800/1073741824 53%2014-06-05T17:11:54.039+0800 [initandlisten] File Preallocator Progress: 807403520/1073741824 75%2014-06-05T17:11:57.026+0800 [initandlisten] File Preallocator Progress: 1069547520/1073741824 99%2014-06-05T17:12:01.985+0800 [FileAllocator] allocating new datafile data/db/local.ns, filling with zeroes...2014-06-05T17:12:01.985+0800 [FileAllocator] creating directory data/db/_tmp2014-06-05T17:12:02.285+0800 [FileAllocator] done allocating datafile data/db/local.ns, size: 16MB, took 0.167 secs2014-06-05T17:12:02.315+0800 [FileAllocator] allocating new datafile data/db/local.0, filling with zeroes...2014-06-05T17:12:02.413+0800 [FileAllocator] done allocating datafile data/db/local.0, size: 64MB, took 0.097 secs2014-06-05T17:12:02.434+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }2014-06-05T17:12:02.434+0800 [initandlisten] added index to empty collection2014-06-05T17:12:02.456+0800 [initandlisten] command local.$cmd command: create { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 numYields:0 reslen:37 494ms2014-06-05T17:12:02.457+0800 [initandlisten] waiting for connections on port 9352
The MongoDB server is successfully started. Listen to port 9352 and wait for the client to connect. If a log file is specified, these logs are output to the specified log file. If it is set as a daemon, a sub-process is created. After the MongoDB server is started, the parent process exits.
It is important to stop MongoDB securely because some data is still cached and not written to the disk. To stop it securely, first write the data to the disk and then end the MongoDB process. You can directly use ctrl + c to stop the MongoDB server, or use the client. mongo is a javascript shell that automatically connects to the MongoDB server when it is started. Therefore, mongo is also a MongoDB client, it can run any javascript program or operate the database. Let's take a look at how mongo stops the MongoDB Server:
[tp0352@server0 bin]$ ./mongo --port 9352MongoDB shell version: 2.6.1connecting to: 127.0.0.1:9352/test> show dbsadmin (empty)local 0.078GBtest (empty)> use adminswitched to db admin> db.shutdownServer()2014-06-05T17:21:59.263+0800 DBClientCursor::init call() failedserver should be down...2014-06-05T17:21:59.265+0800 trying reconnect to 127.0.0.1:9352 (127.0.0.1) failed2014-06-05T17:21:59.266+0800 warning: Failed to connect to 127.0.0.1:9352, reason: errno:111 Connection refused2014-06-05T17:21:59.266+0800 reconnect 127.0.0.1:9352 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:9352 (127.0.0.1), connection attempt failed> exitbye
We can see from the above that the connection to the local port 9352 test database, admin is a root database, this database is used to manage the entire MongoDB, not anyone can stop the MongoDB server, so we need to use admin to execute shutdownServer () to stop.
2). Security and authentication
The above uses the mongo client to connect to the MongoDB server without any security authentication. That is to say, anyone can connect to the MongoDB server. Of course, you can run it in a trusted environment, ensure that only trusted machines can access the service or authenticate a single connection. Each MongoDB database can have many users. If security check is enabled, only database authenticated users can perform related operations. Admin users can perform read and write operations on any database. Users of other databases can only perform operations with relevant permissions.
Before enabling security check, create several users:
> use adminswitched to db admin> db.addUser("tp", "12345")WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' insteadSuccessfully added user: { "user" : "tp", "roles" : [ "root" ] }> use testswitched to db test> db.addUser("test1", "12345", true)WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' insteadSuccessfully added user: { "user" : "test1", "roles" : [ "read" ] }> db.addUser("test2", "12345")WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' insteadSuccessfully added user: { "user" : "test2", "roles" : [ "dbOwner" ] }> use adminswitched to db admin> show collectionssystem.indexessystem.userssystem.version> db.system.users.find(){ "_id" : "admin.tp", "user" : "tp", "db" : "admin", "credentials" : { "MONGODB-CR" : "e4aafd1c8d19d9e490192fe5bf43ffe0" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }{ "_id" : "test.test1", "user" : "test1", "db" : "test", "credentials" : { "MONGODB-CR" : "2da1438b5a04dd46cfdf97c40a3c6d71" }, "roles" : [ { "role" : "read", "db" : "test" } ] }{ "_id" : "test.test2", "user" : "test2", "db" : "test", "credentials" : { "MONGODB-CR" : "4accbd2cb1dce25ed8b3b162103b0b87" }, "roles" : [ { "role" : "dbOwner", "db" : "test" } ] }
A user tp is added to admin, and two users test1 and test2 are added to the test database. addUser () has three parameters. The first parameter is the user name, and the second parameter is the password, the third is the read-only status. Set true to read-only. From the user collection system. users, we can see that tp is the super user in the admin database and has the root permission. test1 has only the read permission, and test2 is the dbOwner, which can be read or written. AddUser can also modify the user password and read-only status. Restart the MongoDB server, add the -- auth Command Option to enable the security check, and then connect to the MongoDB through mongo:
> show dbs2014-06-05T17:59:57.519+0800 listDatabases failed:{ "ok" : 0, "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }", "code" : 13} at src/mongo/shell/mongo.js:47> use testswitched to db test> db.auth("test1", "12345")1> show collectionspeoplesystem.indexes> db.people.find(){ "_id" : ObjectId("53903f7af73bb0df22f8e4a6"), "name" : "Mary", "age" : 10 }> db.people.insert({"name" : "join", "age" : 20})WriteResult({ "writeError" : { "code" : 13, "errmsg" : "not authorized on test to execute command { insert: \"people\", documents: [ { _id: ObjectId('53903fd6959e902814f56d8c'), name: \"join\", age: 20.0 } ], ordered: true }" }})> db.auth("test2", "12345")1> db.people.find(){ "_id" : ObjectId("53903f7af73bb0df22f8e4a6"), "name" : "Mary", "age" : 10 }> db.people.insert({"name" : "join", "age" : 20})WriteResult({ "nInserted" : 1 })> use adminswitched to db admin> db.auth("tp", "12345")1> show dbsadmin 0.078GBlocal 0.078GBtest 0.078GB
After connecting to the MongoDB server, no user authentication is performed, so nothing can be done. After using the test database and performing user authentication, you can view the set in the test database, because test1 can only read and write, therefore, "find" can be successful but "insert" fails. test2 can both read and write data. After using the admin database for user authentication, you can view the list of all databases. In fact, this authentication is not very useful. It is only a method. The safest way is to restrict access.
3). Data Backup
①. Data file backup
The simplest backup is the backup of data files, that is, directly assigning values to the data/db directory, because we have previously specified the data directory as data/db, so there is a lot of data in MongoDB here, but there is a problem that the latest data is still in the cache and has not been synchronized to the disk. You can stop shutdownServer () before backing up. However, this will affect the normal operation of MongoDB.
②. Mongodump and mongorestore
There are two executable files, mongodump and mongorestore, in the bin. This is a backup of A MongoDB database, which can be performed when MongoDB is running, such as backing up the test database, then, import the backed up database file to another MongoDB server. This backup method backs up not the latest data, but data that has been written to MongoDB. Some data may not be written to the cache, so this data cannot be backed up. Mongodump and mongorestore can also query all options through -- help.
[tp0352@server0 bin]$ ./mongodump --port 9352 -d test -o test_dataconnected to: 127.0.0.12014-06-05T19:00:03.381+0800 DATABASE: test to test_data/test2014-06-05T19:00:03.411+0800 test.system.indexes to test_data/test/system.indexes.bson2014-06-05T19:00:03.426+0800 1 documents2014-06-05T19:00:03.426+0800 test.test to test_data/test/test.bson2014-06-05T19:00:03.463+0800 1 documents2014-06-05T19:00:03.464+0800 Metadata for test.test to test_data/test/test.metadata.json
-D indicates the database,-o indicates the output backup file, and the test database is backed up as the test_data file.
[tp0352@server0 bin]$ ./mongorestore --port 9352 -d temple --drop test_data/test/connected to: 127.0.0.1:93522014-06-05T19:05:53.454+0800 test_data/test/people.bson2014-06-05T19:05:53.454+0800 going into namespace [temple.people]2014-06-05T19:05:53.454+0800 dropping2 objects found2014-06-05T19:05:53.454+0800 Creating index: { key: { _id: 1 }, name: "_id_", ns: "temple.people" }[tp0352@server0 bin]$ ./mongo --port 9352MongoDB shell version: 2.6.1connecting to: 127.0.0.1:9352/test> show dbsadmin 0.078GBlocal 0.078GBtemple 0.078GBtest 0.078GB
Here, we will re-import the test database backed up above to the temple database. -- drop indicates that if the temple database is available, all the sets will be deleted, if this parameter is not specified, it is merged with the set in the original temple.
③. Export export and Export Import
As mentioned above, mongodump and mongorestore are used to back up a database. Then, export and Export Import are used to back up a table in a database. You can also use -- help to view all the options, of course, the snapshot export can also be a non-statistical backup, but it is not necessarily the latest data.
[tp0352@server0 bin]$ ./mongoexport --port 9352 -d test -c people -o prson connected to: 127.0.0.1:9352exported 2 records[tp0352@server0 bin]$ cat prson { "_id" : { "$oid" : "53903f7af73bb0df22f8e4a6" }, "name" : "Mary", "age" : 10 }{ "_id" : { "$oid" : "53903ff6959e902814f56d8d" }, "name" : "join", "age" : 20 }[tp0352@server0 bin]$ ./mongoimport --port 9352 -d temple -c user prson connected to: 127.0.0.1:93522014-06-05T19:14:00.555+0800 imported 2 objects[tp0352@server0 bin]$ ./mongo --port 9352MongoDB shell version: 2.6.1connecting to: 127.0.0.1:9352/test> use templeswitched to db temple> show collectionssystem.indexesuser> db.user.find(){ "_id" : ObjectId("53903f7af73bb0df22f8e4a6"), "name" : "Mary", "age" : 10 }{ "_id" : ObjectId("53903ff6959e902814f56d8d"), "name" : "join", "age" : 20 }
-C indicates the collection set. The people set in the test database is backed up as a prson file and then imported to the user set in the temple database.
④. Fsync and lock
Mongodump and mongoexport can be backed up on the MongoDB server without stopping, but the real-time data retrieval capability is lost. The fsync command can also back up real-time data without stopping MongoDB, its implementation is actually locking, blocking write operations on the database, then writing the data in the buffer zone to the disk, backing up and then unlocking. During this period, any write operations on the database will be blocked until the lock is released.
> Db. runCommand ({"fsync": 1, "lock": 1}) {"info": "now locked against writes, use db. fsyncUnlock () to unlock "," seeAlso ":" http://dochub.mongodb.org/core/fsynccommand "," OK ": 1 }>> back up between them and execute any insert operation will block> db. fsyncUnlock () {"OK": 1, "info": "unlock completed "}
In my opinion, real-time data is also relative. Locking the database does not allow writing, so the latest data is still not backed up.