A tutorial

Install MongoDB on Mac

Preparing installation packages

Mongo – Linux – x86_64 – ubuntu1804-4.0.2. TGZ

(1) Go to mongodb official website

(www.mongodb.org/downloads)…

(2) Copy the installation package to the /root/project directory

Sudo cp mongo - Linux - x86_64 - ubuntu1804-4.0.2..tgz/root/projectCopy the code

(3) Decompress the installation package

Sudo tar xf/root/project/mongo - Linux - x86_64 - ubuntu1804-4.0.2. TGZCopy the code

(4) Change the name of the decompressed folder to mongodb

Sudo mv/root/project/mongo - Linux - x86_64 ubuntu1804-4.0.2 mongoCopy the code

(5) Create data and log directories

sudo mkdir -pv /root/project/mongodb/data/{mongodb_data,mongodb_log}
Copy the code

Mongodb_dcta is the data directory and mongodb_log is the log directory

(6) Create the mongodb. Log file

sudo vim /root/project/mongodb/data/mongodb_log/mongodb.log
Copy the code

Wq Save and exit

(7) Create configuration files

Create configuration file command:

sudo vim /root/project/mongodb/data/mongodb.conf
Copy the code

Write to file :(I)

port=27017
dbpath=/root/project/mongodb/data/mongodb_data/
logpath=/root/project/mongodb/data/mongodb_log/mongodb.log
fork=true
logappend=true
noauth=true
Copy the code

Save the configuration and exit :wq

(8) Start the mongodb service

sudo /root/project/mongodb/bin/mongod --config /root/project/mongodb/data/mongodb.conf
Copy the code

The following indicates success:

(9) Use Mongo (start mongodb service first)

sudo /root/project/mongodb/bin/mongo
Copy the code

The following indicates success:

(10) Restart the mongodb service

(See Step 8)Copy the code

Tutorial 2

Decompress the installation package

The tar XVF mongo - Linux - x86_64 - ubuntu1804-4.0.2. TGZCopy the code

Modifying folder names

Sudo mv mongo - Linux - x86_64 ubuntu1804-4.0.2 mongoCopy the code

New Folder

~/root/mongodb/bin#Copy the code

Step one:

cd ~/root/mongodb/bin
Copy the code

Step 2:

./mongod --port 27017 --fork--dbpath=/root/mongodb/data --fork --logpath=/root/mongodb/log
Copy the code
/usr/local/mongodb/bin/mongod --port 27017 --fork --dbpath=/mongo/data/mongodb_data/ --logpath=/mongo/data/mongodb_log/mongodb.log --logappend
Copy the code

Split line (unfinished to be added, move to Tutorial 1 & Tutorial 2)


First, MongoDB installation

1. Download the installation package download method:

The curl - O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.9.tgzCopy the code

Decompress the package:

Tar ZXVF mongo - Linux - x86_64-3.2.9. TGZCopy the code

2. Prepare for the installation. Create database folders, log files, and configuration files.

mkdir -p  /usr/local/server/mongodb/data
touch /usr/local/server/mongodb/mongod.log
touch /usr/local/server/mongodb/mongodb.conf
Copy the code

Move mongodb to /usr/local/server/mongdb:

Mv mongo - Linux - x86_64-3.2.9 / * / usr/local/server/mongo /Copy the code

3. Start the mongodb service using either of the following methods: You can add configuration parameters to the command or a configuration file to start the mongodb service:

cd /usr/local/server/mongodb/bin/
Copy the code
./mongod --dbpath=/usr/local/server/mongodb/data --logpath=/usr/local/server/mongodb/mongod.log --logappend --port=27017  --forkCopy the code

If you have configured the parameters in the configuration file, you can use the configuration file to start:

 ./mongod --config /usr/local/server/mongodb/mongodb.conf
Copy the code

Appendix: Add the mongod to the system service before starting the mongodb service

service mongod start|stop|restart
Copy the code

Note: Mongodb can be temporarily added to the system path variable, so that it can be directly started without entering a path, the code is as follows:

export PATH=/usr/local/server/mongodb/bin:$PATH
Copy the code

Then you can check whether it succeeded:

echo $PATH
Copy the code
  1. –dbpath Database path (data file)
--logpath Log file path --master specifies the master machine --slave specifies the slave machine --source specifies the IP address of the master machine --pologSize Specifies the log file size within 64 MB. Because resync is very costly and time consuming, it is best to avoid resync by setting an oplogSize large enough (the default oplog size is 5% of the size of the free disk). -- logAppEnd added at the end of the log file --port enabled port number --fork run in the background --only specifies which database to copy only --slavedelay refers to the interval between slave replication detection --auth whether to authenticate permission login (username and password) --config Location of the configuration fileCopy the code

2. User authorization and management

1. After mongodb is installed, you do not need a password to enter it for the first time, and there is no user. You can enter it directly by shell command, CD go to the bin folder in mongodb directory, and run the./mongo command.

[root@namenode mongodb]#./bin/mongo mongodb shell version: 1.8.2 connecting to: test > use test; switched to db testCopy the code
Db.createuser ({user: "admin", PWD: "admin", userAdminAnyDatabase); "123456",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})Copy the code

Note: After adding users, run the show users command or db.system.users.find() command to check existing users. 3. After adding admin users, close MongoDB and start MongoDB again using permissions. (If so, go to the data/db directory and delete the mongo.lock file.) You can disable it using db.shutdownServer()

./mongod --dbpath=/usr/local/server/mongodb/data --logpath=/usr/local/server/mongodb/mongod.log --fork --auth
Copy the code

Or modify it in the configuration file:

 auth = true
#noauth = true
Copy the code

5, enter mongo shell, use admin database and verify, if not verified, is unable to do any operation.

> use admin > db. Auth ("admin","123456")  > use mydb > db.createUser({user: "root",pwd: "123456",roles: [{ role: "readWrite", db: [root@localhost mongodb]# mongo 127.0.0.1/mydb -uroot -p mongodb shell version: 3.2.9 Enter password: connecting to: 127.0.0.1/ myDB > DB myDB > use mydb switched to DB myDB > show collectionsCopy the code

Then you can add, delete, change and check all kinds of data operations…

Install the MongoDB extension for PHP on Linux

1) Download the latest PHP MongoDB extension source code, which can be found at pecl.php.net/package/mon… .

2) Decompress the package and go to the installation directory

Wget HTTP: / / http://pecl.php.net/get/mongo-1.4.0.tgzCopy the code
The tar - ZXVF mongo - 1.4.0. TGZCopy the code
CD mongo - 1.4.0Copy the code

3) After entering the folder, first run phpize to compile the extended environment

[root @ localhost mongo - 1.4.0] # / usr/bin/phpizeCopy the code
Configuring for:
PHP Api Version: 20121113
Zend Module Api No: 20121212
Zend Extension Api No: 220121212
Copy the code

4) After running, we run the./configure script to configure

./configure --with-php-config=/usr/local/php/bin/php-config && make && make install
Copy the code
## --with-php-config tells the configuration script the path to the php-config programCopy the code

5) When finished, edit your php.ini file to add a line

extension=mongo.so
Copy the code

General default compile PHP ini file/usr/local/PHP/etc/PHP ini to restart Apache/Nginx [or/etc/init. D/PHP – FPM restart] open a phpinfo see mongo module, Verify that the PHP extension of MongoDB is successfully installed.

OK, now you can use PHP to manipulate MongoDB