First, install MongoDB
1. Create mongodb user groups and users
groupadd mongodb
useradd -r -g mongodb -s / sbin / nologin -M mongodb
2. Download the mongodb source package and place the source package in the / usr / local / src / directory
Download page: https://www.mongodb.com/download-center?jmp=nav
Here is mongodb-linux-x86_64-rhel62-3.2.10.tgz
: Https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.10.tgz
3. Enter the src / directory
1 cd / usr / local / src /
4. Unzip the source package
tar -zxf mongodb-linux-x86_64-rhel62-3.2.10.tgz
5. Create mongodb file directory
mkdir -p / usr / local / mongodb / data
mkdir -p / usr / local / mongodb / conf
mkdir -p / var / run / mongodb
mkdir -p / var / log / mongodb
6. Copy the files to the mongodb / directory
1 cp -R /usr/local/src/mongodb-linux-x86_64-rhel62-3.2.10/. / Usr / local / mongodb
7. Create mongodb configuration file mongodb.conf
vim /usr/local/mongodb/conf/mongodb.conf
8. Add the following content, save and exit
dbpath = / usr / local / mongodb / data #Data directory exists
logpath = / var / log / mongodb / mongodb.log #log file storage directory
logappend = true # Mode of writing logs: set to true for append
fork = true #Enable as a daemon, that is, run in the background
verbose = true
vvvv = true #Enable verbose verbose information, its levels are vv ~ vvvvv, the more v the higher the level, the more detailed the information recorded in the log file
maxConns = 20000 #Default value: depends on system (ie ulimit and file descriptor) limits. MongoDB does not restrict its own connections
pidfilepath = / var / run / mongodb / mongodb.pid
directoryperdb = true #Data directory storage mode, if you modify the original data directly, it will disappear
profile = 0 #Database analysis level setting, 0 off 2 on. Includes all operations. 1 On. Include only slow operations
slowms = 200 #Record the slow query time of profile analysis. The default is 100 milliseconds.
quiet = true
syncdelay = 60 #The frequency of flashing data to the log, and operating the data through fsync. 60 seconds by default
# port = 27017 #Port
#bind_ip = 10.1.146.163 #IP
# auth = true #Start authentication
# nohttpinterface = false # 28017 The service opened on the port. Default false, support
# notablescan = false # Do not prohibit table scan operations
# cpu = true #Set to true to force mongodb to report CPU utilization and io wait every 4s, and write log information to standard output or log file
9. Modify mongodb directory permissions
chown -R mongodb: mongodb / usr / local / mongodb
chown -R mongodb: mongodb / var / run / mongodb
chown -R mongodb: mongodb / var / log / mongodb
10. Add mongodb commands to environment variables and modify the profile
vim / etc / profile
11. Modify to the following content, save and exit
PATH = / usr / local / mysql / bin: / usr / local / php / bin: / usr / local / redis / bin: / usr / local / mongodb / bin: $ PATH
12. Make the configuration in / etc / profile take effect immediately
source / etc / profile
13. Add the mongodb service script to the init.d / directory and create a mongod file
vim /etc/init.d/mongod
14. Add the following content, save and exit
View Code
15. Add executable permissions to mongod
chmod + x /etc/init.d/mongod
16. Add mongodb to the system service
chkconfig --add mongod
17. Modify the default startup level of the service
chkconfig mongod on
18. Start mongodb
service mongod start
Second, PHP7 install MongoDB extension
1. Download the php7 mongodb expansion package and place the source package in the / usr / local / src / directory
Download page: http://pecl.php.net/package/mongodb
Here is mongodb-1.1.9.tgz
: Http://pecl.php.net/get/mongodb-1.1.9.tgz
2. Enter the src / directory
cd / usr / local / src /
3. Unzip the expansion pack
tar -zxf mongodb-1.1.9.tgz
4. Enter the mongodb expansion directory, compile and install the expansion
cd mongodb-1.1.9 /
phpize
./configure --with-php-config = / usr / local / php / bin / php-config
make && make install
5. Modify the php.ini file
vim /usr/local/php/etc/php.ini
6. Add mongodb.so extension configuration, save and exit
extension = mongodb.so
7. Restart Apache or php-fpm
service httpd restart
service php-fpm restart
8. Add a php file in the web directory, such as /usr/local/apache/htdocs/mongodb.php or /usr/local/nginx/html/mongodb.php
<? php
$ manager = new MongoDB \ Driver \ Manager ("mongodb: //127.0.0.1: 27017");
$ bulk = new MongoDB \ Driver \ BulkWrite;
$ bulk-> insert ([‘x’ => 1, ‘class’ == ‘toefl’, ‘num’ => ‘18’]);
$ bulk-> insert ([‘x’ => 2, ‘class’ => ’ielts’, ‘num’ => ‘26’]);
$ bulk-> insert ([‘x‘ => 3, ‘class’ => ‘sat’, ‘num’ => ‘35’]);
$ manager-> executeBulkWrite (‘test.log’, $ bulk);
$ filter = [‘x’ => [‘$ gt’ => 1]];
$ options = [
‘Projection’ => [‘_id’ => 0],
‘Sort’ => [‘x’ => -1],
];
$ query = new MongoDB \ Driver \ Query ($ filter, $ options);
$ cursor = $ manager-> executeQuery (‘test.log’, $ query);
foreach ($ cursor as $ document) {
print_r ($ document);
}
Visit URL, such as: http://192.168.8.9/mongodb.php
If the page is displayed normally, the configuration is successful
MongoDB is installed!
Next Article: Setting Up LNAMP Environment (7)-PHP7 Source Installation Memcached and Memcache Extension
Build LNAMP environment (6)-PHP7 source install MongoDB and MongoDB extension