Linux mongodb installation and data import and export tutorial, mongodb Import and Export
How to install mongodb and import and export data in Linux
# Viewing the linux release version
Cat/etc/issue
# View the Linux kernel version
Uname-r
1. General steps for installing mongodb in Linux
1. Go to the mongodb official website (https://www.mongodb.org/downloads) to download the corresponding installation package for your system, copy (you can use ftp tools such as winscp) to your linux system.
2. decompress the corresponding installation package
Command: tar zxvf mongodb-linux-x86_64-3.0.4.tgz
Renaming the decompressed folder simplifies management: mv mongodb-linux-x86_64-3.0.4 mongodb
Move the mongodb folder to the/usr/local/directory to facilitate management of mv mongodb/usr/local/
3. Create the location where the database is stored and the log file for mongodb, which is under/data/db by default. For convenience, create a directory under the mongodb directory.
[Root @ localhost mongodb] # mkdir data
[Root @ localhost mongodb] # mkdir logs
4. Start the mongodb database. (Note: if the mongodb is decompressed by the root user, the common user cannot start mongo and has no permission to access the files created by the root user)
You can go to the bin directory under mongodb to view the mongodb help documentation and the startup parameters we use:./mongod-h
Start the database (-- fork indicates that the mongo service is run in the background. If it is not set to run in the background, nothing else can be entered in the console)
./Mongod -- dbpath =/usr/mongodb/data/-- logpath =/usr/mongodb/logs/log -- fork
View mongo Processes
Netstat-tlnup | grep release d
To ensure security, we recommend that you disable port 28017 to prevent information leakage.
Pkill mongod
5. log on to the mongodb client to start the mongodb journey (if the background is not set for running in the previous step, re-open a client to enter the following directory for execution)
Run./mongo in the bin directory of the mongodb directory.
This is the case when a password is set for Logon (shyx is the user name, defined in advance. The password shyx can not be written after-p, and will prompt you to enter it later)
./Mongo-u yxadmin-p yx2345 admin
./Mongo-u yxadmin-p this is the most concise login method (note that only the test database is logged on by default), a prompt will be prompted to enter the password
./Mongo 127.0.0.1: 27017/admin-uyxadmin-pyx2345 specify ip address, database, and account
6. Stop mongo (try not to kill the process and cause problems. Use the method in admin to disable it)
> Use admin
Switched to db admin
> Db. shutdownServer ();
Server shocould be 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 (in this way, do not start the service in the installation directory every time)
Vim/etc/profile
PATH = $ PATH:/usr/local/mongodb/bin
Source/etc/profile
8. Set auto-start upon startup
Add the mongodb startup project to rc. local to ensure that mongodb is started when the server is started.
Echo "/usr/local/mongodb/bin/mongod -- dbpath =/usr/local/mongodb/data -- logpath =/usr/local/mongodb/logs/logfile -- logappend-port = 27017 ">/etc/rc. local
(Password Logon is not required for the above statement)
9. Set the logon mode that requires permissions. the user name and password are required for user connection.
Run this command in the bin directory. /mongod -- dbpath =/usr/local/mongodb/data -- logpath =/usr/local/mongodb/logs/logfile -- logappend -- auth -- port = 27017 -- fork
Data can be queried only when the-auth parameter is included. If the-auth parameter is not added, you do not need to authenticate the user even if the user is configured with security authentication.
10. Set the security authentication user and password
By default, apsaradb for MongoDB does not have a user name or password and does not require security verification. You can perform CRUD operations as long as you connect to the service.
If you have already set mongo to start and do not need a password to log on, you need to modify the original boot settings to set the password authentication to the boot service.
========================================================== ==========
After mongo is installed, no user authentication is required by default. You need to create an administrator user:
Use admin
Db. createUser (
{
User: "yxadmin ",
Pwd: "yx2345 ",
Roles: ["root"]
})
Create a pcmddo database under the Administrator account and allocate the user and password of the database (the administrator can operate any database)
Use pcmddo
Db. createUser (
{
User: "yxkj ",
Pwd: "yx2345 ",
Roles:
[
{Role: "readWrite", db: "pcmddo "},
]
}
)
========================================================== ============
Go to mongodb \ bin and use./mongo to mongodb command line management.
Type the following command:
Use admin
Db. createUser (
{
User: "yxkj ",
Pwd: "yx2345 ",
Roles :[
{
Role: "userAdminAnyDatabase ",
Db: "admin"
}]
}
);
Now we add a user shyx to the mongodb admin database, and the password is also shyx. mongodb can establish permission authentication for each database, that is, you can specify which database a user can log on. The above Code adds a shyx user to the admin database. In mongodb, the admin database is a special database. Users of this database can access all the databases in mongodb.
If you want to set a user for the test database, run the following command:
Use test
Db. createUser (
{
User: "yxkj ",
Pwd: "yx2345 ",
Roles :[
{
Role: "userAdminAnyDatabase ",
Db: "admin"
}]
}
);
Now we have set a global user yxkj for mongodb. Next, restart mongodb to make the created user take effect (/etc/init. d/mongod restart)
Possible problems
When the command/usr/local/mongodb/bin/mongod -- dbpath =/usr/local/mongodb/data -- logpath =/usr/local/mongodb/logs/mongod is used. when log -- fork starts mongodb, the following error is reported: child process failed, exited with error number 1
This error is caused by abnormal mongodb shutdown. The solution is as follows:
Delete the following file:/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
II. Introduction to Mongodb Data Import and Export
On Windows, no user name or password is set
Export Import-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
Export export-d Test_DBtsv-c Test_Table -- csv-f mobile phone number, field2, field3, field4, field7-o D: \ data \ outdata_csv.txt
Import and export data on the Linux platform and after User Authentication
Export Import-d Test_DBcsv-c Test_Table -- type csv-h 127.0.0.1-u zhou-p 123 -- fields mobile phone number, field2, field3, field4, field5, field6, field7 -- file/data/test.txt
Export export-d Test_DBtsv-c Test_Table -- csv-u zhou-p 123-f mobile phone number, field2, field3, field4, field7-o/data/test.txt
Fields can be specified for data export.
The two platforms use different local file paths. If you want to log on with your username and password, write the corresponding parameters (-u-p )!
The premise for user authentication is to assign the account password to the database Test_DBcsv in advance:
If you do not use the -- auth option to start mongo, go to mongo shell and execute the following statement.
Use Test_DBcsv
Db. createUser (
{
User: "zhou ",
Pwd: "123 ",
Roles:
[
{Role: "readWrite", db: "Test_DBcsv "},
]
}
)
In this way, the account zhou can be used to log on to the database Test_DBcsv when the -- auth option is used to start the mongo server.
References:
Http://chenzhou123520.iteye.com/blog/1641319
View Command help
Export Import -- help
Export export -- help
Mysql import data:
Load data infile "/big/pcmd5days/nanjing620/hebingnew.txt" into table table2 (time, processdate, ismi, lac, ceid, seg, importdate );
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.