Linux under the mongodb installation and data import and export tutorial
# View linux distributions
Cat/etc/issue
# View the linux kernel version number
Uname-r
I,Linux under The general steps of MongoDB installation
1. Go to MongoDB 's website (https://www.mongodb.org/downloads) to download the appropriate installation package for your system , Copy ( can use ftp tools like winscp) to your linux system.
2. Unzip the appropriate installation package
Commands such as the following : Tar zxvf mongodb-linux-x86_64-3.0.4.tgz
Rename the extracted directory to simplify management : MV mongodb-linux-x86_64-3.0.4 MongoDB
Move the MongoDB folder to the /usr/local/ folder for easy management of MV mongodb/usr/local/
3. Create a database location and log file for MongoDB , The default is below /data/db , The same folder is set up below the MongoDB folder for convenience.
[Email protected] mongodb]# mkdir data
[Email protected] mongodb]# mkdir logs
4. Start mongodb database ( note : assumed to be root user extracted under mongo mongo root user-created files
The ability to go to the Bin folder below MongoDB to view MongoDB 's help documentation and the number of startup parameters we used:. Mongod-h
Start the database (--fork indicates that the MONGO service is executed in the background . Assuming not set to background execution will not be able to enter anything else in the console )
./mongod--dbpath=/usr/mongodb/data/--logpath=/usr/mongodb/logs/log--fork
View the MONGO process
Netstat-tlnup | grep Mongod
For security reasons, it is recommended to close 28017port to prevent information leakage
Pkill Mongod
5. Enter MongoDB 's client to start the mongodb tour ( Assuming that there is no background execution in the previous step, you have to open a client again to enter the following folder execution )
Run under the bin folder under the mongodb folder ./mongo
Set the password login situation to write this (Shyx is username, predefined ,password Shyx can not write after-p. You will be prompted to enter later )
./mongo-u yxadmin-p yx2345 Admin
./mongo-u Yxadmin-p This is the simplest way to log in ( Note that the default is simply to log in to the test Database ), will prompt for input password
./mongo 127.0.0.1:27017/admin-uyxadmin-pyx2345 Specify IP, database, account
6. Stop MONGO ( try not to kill the process, which can cause problems.) Use the admin method to close )
> Use admin
Switched to DB admin
> Db.shutdownserver ();
Server should is down ...
You can use Ps-ef or ps-aux to View the process, find the MONGO process number , and Kill
7. Add MONGO to the environment variable ( So don't start the service every time you go into the installation folder )
Vim/etc/profile
Path= $PATH:/usr/local/mongodb/bin
Source/etc/profile
8. Set up boot from boot
Add MongoDB startup item rc.local to ensure MongoDB starts when server is powered on
echo "/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data--logpath=/usr/local/mongodb/logs/logfile-- logappend-port=27017 ">>/etc/rc.local
(The above statement is not set to require password login )
9. Set the required login mode , user connection need username and password
start with this command under the Bin folder ./mongod--dbpath=/usr/local/mongodb/data--logpath=/usr/local/mongodb/logs /LogFile--logappend--auth--port=27017--fork
With -auth , you must be able to query the data through the certification competency.
Assume that there are no -auth parameters, even if a secure authenticated user is configured. There is no need to certify who can operate.
10. Set up secure authentication users and password
The MongoDB database is not real username and password by default. Without security verification, it is possible to perform CRUD operations simply by connecting to the service .
Assuming that you have previously set up boot MONGO and do not have password login, then to change the original boot start settings, set up to require password authentication of the boot service.
================================================
After the installation of MONGO is not required by default user authentication, to create an administrator user:
Use admin
Db.createuser (
{
User: "Yxadmin",
PWD: "yx2345",
roles:["Root"]
} )
Create a Pcmddo database under the Administrator account and assign users and password to the database (administrators can manipulate arbitrary databases)
Use Pcmddo
Db.createuser (
{
User: "Yxkj",
PWD: "yx2345",
Roles
[
{role: "ReadWrite", DB: "Pcmddo"},
]
}
)
==================================================
Navigate to Mongodb\binand use ./mongo to enter mongodb 's command line management.
Type the command :
Use admin
Db.createuser (
{
User: "Yxkj",
PWD: "yx2345",
Roles: [
{
Role: "Useradminanydatabase",
DB: "Admin"
} ]
}
);
Now we areMongoDBof theAdmindatabase joins a userShyx, Password isShyx,MongoDBThe ability to establish permission authentication for each database, that is, you can specify which database a user can log into. The code above. We areAdminthe database joins aShyxusers, inMongoDBinAdmindatabase is a special database, the user of this database, can access to askMongoDBall databases in the.
Suppose you want to set up a user for the test database, using the following command, for example:
Use test
Db.createuser (
{
User: "Yxkj",
PWD: "yx2345",
Roles: [
{
Role: "Useradminanydatabase",
DB: "Admin"
} ]
}
);
Well, now that we've set up a global user yxkjfor MongoDB , we'll restart mongodbto make the created user effective (/etc/init.d/mongod restart)
Problems that may occur
When using commands /usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data--logpath=/usr/local/mongodb/logs/ Mongod.log--fork when starting MongoDB , the following error is reported for example:childprocess failed, exited with error Number 1
This is due to an abnormal shutdown of MongoDB caused by the error, the workaround method is as follows:
Delete the following file to:/usr/local/mongodb/mongod.lock
References :
Http://jingyan.baidu.com/article/fd8044faf4f3a95030137a79.html
http://blog.csdn.net/yuwenruli/article/details/8529192
Http://www.2cto.com/database/201408/322384.html
http://docs.mongodb.org/manual/reference/method/db.createUser/#create-administrative-user-with-roles
Second, the Mongodb data import and export function introduction
Windows platform, no settings Usernamepassword
mongoimport-d test_dbtsv-c test_table--type tsv-h 127.0.0.1--fields mobile phone number , FIELD2,FIELD3,FIELD4,FIELD5, Field6,field7,field8--file D:\data\test.txt
mongoexport-d test_dbtsv-c test_table--csv-f Mobile number , Field2,field3,field4,field7-o D:\data\outdata_ Csv.txt
Linux platform, user authentication after import, export data
mongoimport-d test_dbcsv-c test_table--type csv-h 127.0.0.1-u zhou-p 123--fields mobile phone number , Field2,field3,fiel D4,field5,field6,field7--file/data/test.txt
mongoexport-d test_dbtsv-c test_table--csv-u zhou-p 123-f mobile phone number , Field2,field3,field4,field7-o/data/test . txt
Export data to specify fields
The two platforms are mostly local file paths that do not have the same wording. Suppose you want to log in with Usernamepassword to write the corresponding number of parameters (-u-p)!
The prerequisite for user authentication is to assign a good account password to the database test_dbcsv in advance:
Enter MONGO Shell to run the following statement without using the --auth option to start MONGO
Use Test_dbcsv
Db.createuser (
{
User: "Zhou",
PWD: "123",
Roles
[
{role: "ReadWrite", DB: "Test_dbcsv"},
]
}
)
This allowsyou to log in to the database using the account Zhou if you start MONGOserver with the --auth option test_ It's dbcsv.
References :
http://chenzhou123520.iteye.com/blog/1641319
View command Use Help
Mongoimport--help
Mongoexport--help
Mysql Import Data:
Load data infile "/big/pcmd5days/nanjing620/hebingnew.txt" into table table2 (time, Processdate,ismi,lac,ceid,seg, Importdate);
Linux under the MongoDB installation and data import and export tutorial