Centos7 install mongodb 4.2.9 and PHP extension

Time:2021-1-17

Visit mongodb website https://www.mongodb.com/try/download/community , there are options on the right. Here, select centos7 + tgz + 4.2.9.

[[email protected] src]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.9.tgz
--2020-09-29 15:01:18--  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.9.tgz
Resolving host fastdl.mongodb.org  ( fastdl.mongodb.org )... 204.246.164.124, 204.246.164.81, 204.246.164.16, ...
Connecting fastdl.mongodb.org  ( fastdl.mongodb.org )|204.246.164.124 |: 443... Connected.
HTTP request issued, waiting for response... 200 OK
Length: 132776427 (127m) [application / gzip]
Saving to: mongodb-linux-x86_ 64-rhel70-4.2.9.tgz”

100% [= = = = = = = = = = = = = = = = = = = = = = = = = = = 100%] 132776427 9.48mb/s, 14s

2020-09-29 15:01:33 (9.37 MB / s) - saved "mongodb-linux-x86"_ 64-rhel70-4.2.9.tgz” [132776427/132776427])
[[email protected] src]# tar zxvf mongodb-linux-x86_64-rhel70-4.2.9.tgz
[[email protected] src]# mv mongodb-linux-x86_64-rhel70-4.2.9 /usr/local/mongodb
[[email protected] src]# cd /usr/local/mongodb/
[[email protected] mongodb]# mkdir -p ./data/db
[[email protected] mongodb]# mkdir log
[[email protected] mongodb]# mv log logs
[[email protected] mongodb]# touch ./logs/mongodb.log

New profile:

[[email protected] mongodb]# vim  mongodb.conf
#Port number
port=27017
#DB directory
dbpath=/usr/local/mongodb/data/db
#Log directory
logpath=//usr/local/mongodb/logs/mongodb.log
#Backstage
fork=true
#Log output
logappend=true
#Allow remote IP connection
bind_ip=0.0.0.0

 

Start mongodb:

[[email protected] mongodb]# ./bin/mongod --config mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 17024
child process started successfully, parent exiting

 

Connect mongodb:

[[email protected] mongodb]# ./bin/mongo
MongoDB shell version v4.2.9
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2e0543e1-1f66-44cf-bfd3-3af38f6abe80") }
MongoDB server version: 4.2.9
Server has startup warnings:
2020-09-29T15:03:49.377+0800 I  STORAGE  [initandlisten]
2020-09-29T15:03:49.377+0800 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-09-29T15:03:49.377+0800 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-09-29T15:03:50.166+0800 I  CONTROL  [initandlisten]
2020-09-29T15:03:50.166+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-09-29T15:03:50.166+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-09-29T15:03:50.166+0800 I  CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-09-29T15:03:50.166+0800 I  CONTROL  [initandlisten]
2020-09-29T15:03:50.167+0800 I  CONTROL  [initandlisten]
2020-09-29T15:03:50.167+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-09-29T15:03:50.167+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-09-29T15:03:50.167+0800 I  CONTROL  [initandlisten]
2020-09-29T15:03:50.167+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-09-29T15:03:50.167+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-09-29T15:03:50.167+0800 I  CONTROL  [initandlisten]
2020-09-29T15:03:50.167+0800 I  CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 100001 files. Number of processes should be at least 50000.5 : 0.5 times number of files.
2020-09-29T15:03:50.167+0800 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
#Select database
> use admin
switched to db admin
> show databases;
admin   0.000GB
config  0.000GB
Local 0.000gb # create root user
> db.createUser({user:"root",pwd:"freedom",roles:[{role:"userAdminAnyDatabase",db:"admin"},"readWriteAnyDatabase"]})
Successfully added user: {
    "user" : "root",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        },
        "readWriteAnyDatabase"
    ]
}

 

Disconnect the reconnection link and enter the verification information:

[[email protected] mongodb]# ./bin/mongo
> use admin
switched to db admin
> db.auth('root','freedom')
1

 

To configure the service file:

[[email protected] mongodb]# touch /usr/lib/systemd/system/mongod.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/conf/mongodb.conf
User=root
Group=root
PrivateTmp=true
Restart=always
RestartSec=1

 

Restart SYSTEMd service and mongodb:

[[email protected] mongodb]# chmod +x /usr/lib/systemd/system/mongod.service
[[email protected] mongodb]# systemctl daemon-reload
[[email protected] ~]# systemctl status mongod
● mongod.service - mongodb
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; vendor preset: disabled)
   Active: active (running) since 2020-09-29 15:44:53 CST; 5S ago
  Process: 25525 ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf (code=exited, status=0/SUCCESS)
 Main PID: 25527 (mongod)
   CGroup: /system.slice/mongod.service
           └─25527 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf

September 29 15:44:52 Guangzhou system D: starting mongodb
September 29 15:44:52 Guangzhou mongod [25525]: about to fork child process, waiting until server is ready for connections
September 29 15:44:52 Guangzhou mongod [25525]: forced process: 25527
September 29 15:44:53 Guangzhou mongod [25525]: child process started successfully, parent existing
September 29 15:44:53 Guangzhou system D: started mongodb
[[email protected] ~]# systemctl stop mongod
[[email protected] ~]# systemctl status mongod
● mongod.service - mongodb
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

September 29 15:33:54 Guangzhou system D: Unit mongod.service  entered failed state.
September 29 15:33:54 Guangzhou SYSTEMd [1]: mongod.service  failed.
September 29 15:44:52 Guangzhou system D: starting mongodb
September 29 15:44:52 Guangzhou mongod [25525]: about to fork child process, waiting until server is ready for connections
September 29 15:44:52 Guangzhou mongod [25525]: forced process: 25527
September 29 15:44:53 Guangzhou mongod [25525]: child process started successfully, parent existing
September 29 15:44:53 Guangzhou system D: started mongodb
September 29 15:45:04 Guangzhou system D: stopping mongodb
September 29 15:45:04 Guangzhou mongod [25625]: killing process with PID: 25527
September 29 15:45:05 Guangzhou system D: stopped mongodb

Client shortcut creation:

[[email protected] mongodb]# ln -s /usr/local/mongodb/bin/mongo /usr/local/bin/mongo
[[email protected] mongodb]# mongo -version
MongoDB shell version v4.2.9
git version: 06402114114ffc5146fd4b55402c96f1dc9ec4b5
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64

Start mongodb: systemctl enable mongod

Start mongodb manually: systemctl start mongod

Stop mongodb: systemctl stop mongod

View running status: systemctl status mongod

 

To install mongodb’s PHP extension:

[[email protected] src]# wget https://pecl.php.net/get/mongodb-1.7.4.tgz
[[email protected] src]# tar -zxvf  mongodb-1.7.4.tgz && cd mongodb-1.7.4/
[[email protected] mongodb-1.7.4]# /usr/local/php/bin/phpize
[[email protected] mongodb-1.7.4]# ./configure --with-php-config=/usr/local/php/bin/php-config
[[email protected] mongodb-1.7.4]# make && make install
[[email protected] mongodb-1.7.4]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/
Total consumption 36028
-Rwxr-xr-x 1 root 602416 January 30 2020 amqp.so
-Rwxr-xr-x 1 root 377328 December 1 2019 curl.so
-Rwxr-xr-x 1 root 89december 20, 2016 2019 event.so
-Rwxr-xr-x 1 root 2754408 July 26 19:12 intl.so
-Rwxr-xr-x 1 root 5779432 Oct 5 17:15 mongodb.so
-Rwxr-xr-x 1 root 658504 December 1, 2019 mysqli.so
-Rwxr-xr-x 1 root 4238372 November 26 2019 opcache. A
-Rwxr-xr-x 1 root 2292240 November 26, 2019 opcache.so
-Rwxr-xr-x 1 root 2554184 Dec 21 2019 redis.so
-Rwxr-xr-x 1 root 15008632 December 1, 2019 swoole.so
-Rwxr-xr-x 1 root 1712352 September 23 18:27 spool_ tracker.so
[[email protected] mongodb-1.7.4]# vim /etc/php.ini
#New configuration
extension=mongodb.so
[[email protected] mongodb-1.7.4]# php --ri mongodb

mongodb

MongoDB support => enabled
MongoDB extension version => 1.7.4
MongoDB extension stability => stable
libbson bundled version => 1.16.2
libmongoc bundled version => 1.16.2
libmongoc SSL => enabled
libmongoc SSL library => OpenSSL
libmongoc crypto => enabled
libmongoc crypto library => libcrypto
libmongoc crypto system profile => disabled
libmongoc SASL => disabled
libmongoc ICU => disabled
libmongoc compression => enabled
libmongoc compression snappy => disabled
libmongoc compression zlib => enabled
libmongocrypt bundled version => 1.0.3
libmongocrypt crypto => enabled
libmongocrypt crypto library => libcrypto

Directive => Local Value => Master Value
mongodb.debug => no value => no value