Linux under the MongoDB installation and data import and export tutorial

Source: Internet
Author: User
Tags auth mongo shell

Linux under the mongodb installation and data import and export tutorial

# View linux release number

Cat/etc/issue

# View linux kernel version

Uname-r

I,Linux under The general steps of MongoDB installation

1. Download the installation package for your system to the website of MongoDB (https://www.mongodb.org/downloads) Copy (you can use ftp tools like winscp) to your linux system.

2. Unzip the corresponding installation package

The command is as follows : Tar zxvf mongodb-linux-x86_64-3.0.4.tgz

Rename extracted folders for simplified management : MV mongodb-linux-x86_64-3.0.4 MongoDB

Move the MongoDB folder to the /usr/local/ directory for easy management of MV mongodb/usr/local/

3. Create a database location and log file for MongoDB , the default is under /data/db , The corresponding directory is also set up under the mongodb directory for convenience.

[Email protected] mongodb]# mkdir data

[Email protected] mongodb]# mkdir logs

4. Start mongodb database ( note : If it is root user extracted under the mongo mongo root user-created file )

You can go to the bin directory under MongoDB to view MongoDB 's help documentation and the startup parameters we used:./ Mongod-h

Start the database (--fork indicates that the MONGO service is running in the background and will not be able to enter anything else in the console if not set to background execution )

./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 the 28017 port to prevent information leakage

Pkill Mongod

5. Enter MongoDB client to start MongoDB tour ( If the previous step does not set the background to run, then you have to reopen a client to enter the following directory execution )

Execute under the bin directory in the mongodb directory ./mongo

Set the password login situation to write this (Shyx is the user name , predefined , password shyx can not - P after writing, then prompt for input )

./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 ), You will be prompted to enter your password

./mongo 127.0.0.1:27017/admin-uyxadmin-pyx2345 Specify IP, database, account

6. Stop MONGO ( try not to kill the process, will cause problems, use The method in the admin 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 to the installation directory )

Vim/etc/profile

Path= $PATH:/usr/local/mongodb/bin

Source/etc/profile

8. Set up boot from boot

Add MongoDB startup Project to rc.local to ensure MongoDB starts when the 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 login mode that requires permission , user name and password required for user connection

start with this command in the bin directory ./mongod--dbpath=/usr/local/mongodb/data--logpath=/usr/local/mongodb/logs/ LogFile--logappend--auth--port=27017--fork

With the -auth parameter, authentication is required to query the data. Without the -auth parameter, even if a secure authenticated user is configured, no authentication is required for anyone to operate.

10. Set up secure authentication user and password

MongoDB database in the default is no user name and password, without security authentication, as long as the connection to the service can be CRUD operations.

If you have previously set up boot MONGO and do not have password login, then to modify the original boot start settings, set to require password authentication of the boot service.

================================================

User authentication is not required by default after installing MONGO, 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 passwords for this database (administrators can manipulate any database)

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 Add a userShyx, the password isShyx,MongoDBYou can establish permission authentication for each database, that is, you can specify which database a user can log on to. The above code, we areAdminThe database adds aShyxusers, inMongoDBinAdminThe database is a special database that the user of this database can accessMongoDBall databases in the.

If you want to set up a user for the test database, use the following command:

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 start mongodb , the following error is reported:childprocess failed, exited with error Number 1

This is due to an abnormal shutdown of MongoDB caused by errors, the workaround is as follows:

Remove the following files:/usr/local/mongodb/mongod.lock

Reference documents :

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 user name password set

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 mainly local file path is different, if you want to log in with the user name password to write the corresponding parameters (-u-p)!

The prerequisite for user authentication is to assign the account password to the database test_dbcsv in advance:

Enter the MONGO shell to execute 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 allows you to log in to the database using the account Zhou using the --auth option to start the MONGO server test_dbcsv up.

Reference documents :

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);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux under the MongoDB installation and data import and export tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.