1. 程式人生 > >Mongodb安裝篇(CentOS7,Mongodb4.0.1)

Mongodb安裝篇(CentOS7,Mongodb4.0.1)

Linux下安裝:

1,CentOS安裝 (不要make編譯安裝,下載好,配置好就行了,配置安裝如下)

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.1.tgz

tar -zxvf mongodb-linux-x86_64-4.0.1.tgz

解壓後可以刪除: rm -rf mongodb-linux-x86_64-4.0.1.tgz 

mkdir -p /usr/local/mongodb/

-----------------------注意安裝包解壓的位置------------------------

mv /root/mongodb-linux-x86_64-4.0.1/* /usr/local/mongodb/    

或者

mv /usr/local/mongodb-linux-x86_64-4.0.1/* /usr/local/mongodb/

-----------------------------------------------------------------

移動後可以刪除資料夾: rm -rf mongodb-linux-x86_64-4.0.1

mkdir -p /usr/local/mongodb/data/db/   # mongodb的db要自己建

mkdir -p /usr/local/mongodb/logs/

cd /usr/local/mongodb/logs/

touch mongodb.log                   # mongodb的log要自己建

2,配置:

dbpath=/usr/local/mongodb/data/db

logpath=/usr/local/mongodb/logs/mongodb.log

port=27017

logappend=true

fork=true

bind_ip=0.0.0.0

#auth=true

1,mongodb.conf(這個檔案是沒有的,自己建立需要自己建立並且放在/usr/local/mongodb/bin/目錄下)

cd /usr/local/mongodb/bin/

vim mongodb.conf    #輸入以下配置

  dbpath=/usr/local/mongodb/data/db             #資料檔案存放目錄

  logpath=/usr/local/mongodb/logs/mongodb.log    #日誌檔案存放目錄

  port=27017       #埠,預設27017,可以自定義

  logappend=true   #開啟日誌追加新增日誌

  fork=true        #以守護程式的方式啟用,即在後臺執行

  bind_ip=0.0.0.0  #預設是127.0.0.1,開啟遠端訪問

  #auth=true(這項暫時不動,因為涉及到auth認證,除錯好所有的mongodb的問題後在來弄許可權)

 注意:如果不加後臺啟動引數fork,這個頁面不能關閉,使用命令連線資料庫時要另開啟一個視窗

3,新增mongodb環境:

vi /etc/profile

在檔案最後一行新增:

export MONGODB_HOME=/usr/local/mongodb

export PATH=$PATH:$MONGODB_HOME/bin

source /etc/profile(立即生效)

4.啟動mongodb:

cd /usr/local/mongodb/bin/

mongod --config mongodb.conf   # 或者輸入:

5.開機自動啟動mongodb:

編輯/etc/rc.d/rc.local,

在檔案後面加上如下這行:
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/bin/mongodb.conf

6.新增埠27017:

  cd /etc/sysconfig/ (切換目錄)

 注意:如果沒有iptables這個檔案,因為centos7自帶firewall

   輸入命令:  yum install iptables-services  # 更新或者安裝iptables

   然後: systemctl enable iptables   # 啟動iptables

   最後: systemctl start iptables    # 開啟iptables

 vim iptables    # 添置:

 -A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
 -A INPUT -j REJECT --reject-with icmp-host-prohibited # 預設有的,上一句位置不要改

7.重啟iptable:

 systemctl restart iptables.service (重新啟動)

 systemctl enable iptables.service  (啟動生效)

8,在 cd /usr/local/mongodb/bin/ mongod 會出現:

Error: couldn't connect to server 127.0.0.1:27017

1、若資料庫出現如上不能連線的原因,可能是data目錄下的mongod.lock檔案問題,可以用如下命令修復

 [[email protected] mongodb]# ./bin/mongod --repair

 或者直接刪除mongod.lock

 rm -rf /usr/local/mongodb/data/db/mongod.lock

2、如果第一步解決不了,那麼就是路徑設定有問題,通過重新對路徑的設定解決此問題

 [[email protected] mongodb]# ./bin/mongod --dbpath=/usr/local/mongodb/data/db/

9,出現 MongoDB starting : pid=2079 port=27017 dbpath=/root/mongodb/data/db/ 表示成功

10,使用,測試mongodb

 9後 表示設定成功,此時不要關閉這個介面,需要 xshell 重新開啟一個視窗來啟動 mongo

 cd /usr/local/mongodb

./bin/mongo

注意:mongodb資料庫無法建立和新增資料的問題(這裡不建議使用admin,local,config三個資料庫),直接輸入

 > use test

 > db.test.insert({id:1})

 > show dbs, # 是不是發現多了一個test???問題解決

******************************************************************************************************************************************************

11,常用的快捷鍵:(配置好後,方便使用)

1、若資料庫出現如上不能連線的原因,可能是data目錄下的mongod.lock檔案問題,可以用如下命令修復

 [[email protected] mongodb]# ./bin/mongod --repair

 或者直接刪除mongod.lock

 rm -rf /usr/local/mongodb/data/db/mongod.lock

2,啟動MongoDB:

  cd /usr/local/mongodb/data/db/

   ./bin/mongod   # 建立連線

   ./bin/mongo    # 使用MongoDB   ( Xsell新開視窗 ),輸入sql語句相關

3,也可以通過mongoVUE這個視覺化工具來連線,或者 Navicat 12 for MongoDB

4,停止mongodb

 # cd /usr/local/mongodb/bin

 # ./mongod -shutdown -dbpath=/usr/local/mongodb/data

5,連線mongodb,可以直接使用命令mongo進入mongodb命令列

   # cd /usr/local/mongodb/bin

   # ./mongo

***************************************************************************************************************************************************

12,參考:

# centos7安裝MongoDB4.0

https://blog.csdn.net/MiaodXindng/article/details/81774273

# 可能出現錯誤

https://blog.csdn.net/wangli61289/article/details/44459467  

****************************************************************************************************************************************************

13,特別注意:( mogodb 啟動 與 關閉 )

¥,mogodb 啟動

1,預設的啟動方式

   ./mongod  
   預設的啟動方式,資料儲存目錄裡是/data/db,監聽埠是27017

2,帶基本引數的啟動方式

   ./mongod --port 51017 --dbpath /home/mongo/data --logpath /home/mongo/log/log.log --logappend --fork --journal 1000 

基本引數:
   -f                           指定配置檔案 (參考:http://www.mongodb.org/display/DOCS/File+Based+Configuration)

   --port                       指定埠,預設是27017
   --dbpath                     資料目錄路徑
   --logpath                    日誌檔案路徑
   --logappend                  日誌append而不是overwrite
   --fork                       以建立子程序的方式執行
   --journal                    日誌提交間隔,預設100ms
   --nojournal                  關閉日誌功能,2.0版本以上是預設開啟的

¥,mongodb關閉:

 > use admin
 > db.shutdownServer()
 > db.shutdownServer({force : true}) # 強制關閉Mongod,應對副本集中主從時間差超過10s時不允許關閉主庫的情況

 不要使用kill直接殺mongo程序的方式關閉資料節點,會造成資料損壞

********************************************************** 最終效果圖 **************************************************************************

 xsell   連結   centos 7 

[[email protected] ~]# cd /usr/local/mongodb/
 [[email protected] mongodb]# ll
 總用量 120
 drwxr-xr-x. 2 root root   251 8月  23 14:39 bin
 -rw-r--r--. 1 root root 34520 8月   4 01:43 GNU-AGPL-3.0
 -rw-r--r--. 1 root root  2149 8月   4 01:43 LICENSE-Community.txt
 drwxr-xr-x. 2 root root    25 8月  23 14:35 logs
 -rw-r--r--. 1 root root 16726 8月   4 01:43 MPL-2
 -rw-r--r--. 1 root root  2195 8月   4 01:43 README
 -rw-r--r--. 1 root root 57190 8月   4 01:43 THIRD-PARTY-NOTICES
 [[email protected] mongodb]# cd logs
 [[email protected] logs]# ll
 總用量 4
 -rw-r--r--. 1 root root 1437 8月  23 14:40 mongodb.log
 [[email protected] logs]# 
 [[email protected] logs]# cd
 [[email protected] ~]# 
 [[email protected] ~]# cd /usr/local/mongodb/
 [[email protected] mongodb]# ll
 總用量 120
 drwxr-xr-x. 2 root root   251 8月  23 14:39 bin
 -rw-r--r--. 1 root root 34520 8月   4 01:43 GNU-AGPL-3.0
 -rw-r--r--. 1 root root  2149 8月   4 01:43 LICENSE-Community.txt
 drwxr-xr-x. 2 root root    25 8月  23 14:35 logs
 -rw-r--r--. 1 root root 16726 8月   4 01:43 MPL-2
 -rw-r--r--. 1 root root  2195 8月   4 01:43 README
 -rw-r--r--. 1 root root 57190 8月   4 01:43 THIRD-PARTY-NOTICES
 [[email protected] mongodb]# cd bin
 [[email protected] bin]# ll
 總用量 208816
 -rwxr-xr-x. 1 root root  6906321 8月   4 01:43 bsondump
 -rwxr-xr-x. 1 root root     5792 8月   4 01:59 install_compass
 -rwxr-xr-x. 1 root root 38886336 8月   4 01:59 mongo
 -rwxr-xr-x. 1 root root 60972352 8月   4 01:59 mongod
 -rw-r--r--. 1 root root      151 8月  23 14:39 mongodb.conf
 -rwxr-xr-x. 1 root root 10255092 8月   4 01:43 mongodump
 -rwxr-xr-x. 1 root root  8245971 8月   4 01:43 mongoexport
 -rwxr-xr-x. 1 root root  8123365 8月   4 01:43 mongofiles
 -rwxr-xr-x. 1 root root  8404330 8月   4 01:43 mongoimport
 -rwxr-xr-x. 1 root root  9322864 8月   4 01:43 mongoreplay
 -rwxr-xr-x. 1 root root 11451628 8月   4 01:43 mongorestore
 -rwxr-xr-x. 1 root root 34621336 8月   4 01:59 mongos
 -rwxr-xr-x. 1 root root  8483293 8月   4 01:43 mongostat
 -rwxr-xr-x. 1 root root  8116571 8月   4 01:43 mongotop
 [[email protected] bin]# mongod
 2018-08-23T16:23:30.288+0800 I CONTROL  [initandlisten] MongoDB starting : pid=8416 port=27017 dbpath=/data/db 64-bit host=localhost.localdomain
 2018-08-23T16:23:30.288+0800 I CONTROL  [initandlisten] db version v4.0.1
 2018-08-23T16:23:30.288+0800 I CONTROL  [initandlisten] git version: 54f1582fc6eb01de4d4c42f26fc133e623f065fb
 2018-08-23T16:23:30.288+0800 I CONTROL  [initandlisten] allocator: tcmalloc
 2018-08-23T16:23:30.288+0800 I CONTROL  [initandlisten] modules: none
 2018-08-23T16:23:30.288+0800 I CONTROL  [initandlisten] build environment:
 2018-08-23T16:23:30.288+0800 I CONTROL  [initandlisten]     distarch: x86_64
 2018-08-23T16:23:30.288+0800 I CONTROL  [initandlisten]     target_arch: x86_64
 2018-08-23T16:23:30.288+0800 I CONTROL  [initandlisten] options: {}
 2018-08-23T16:23:30.292+0800 I STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
 2018-08-23T16:23:30.292+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=256M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
 2018-08-23T16:23:31.275+0800 I STORAGE  [initandlisten] WiredTiger message [1535012611:275666][8416:0x7f3078f489c0], txn-recover: Main recovery loop: starting at 5/16128
 2018-08-23T16:23:31.377+0800 I STORAGE  [initandlisten] WiredTiger message [1535012611:377240][8416:0x7f3078f489c0], txn-recover: Recovering log 5 through 6
 2018-08-23T16:23:31.447+0800 I STORAGE  [initandlisten] WiredTiger message [1535012611:447687][8416:0x7f3078f489c0], txn-recover: Recovering log 6 through 6
 2018-08-23T16:23:31.506+0800 I STORAGE  [initandlisten] WiredTiger message [1535012611:506397][8416:0x7f3078f489c0], txn-recover: Set global recovery timestamp: 0
 2018-08-23T16:23:31.522+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] 
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] 
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
 2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] 
 2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] 
 2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
 2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
 2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] 
 2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
 2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
 2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] 
 2018-08-23T16:23:31.557+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
 2018-08-23T16:23:31.558+0800 I NETWORK  [initandlisten] waiting for connections on port 27017
 2018-08-23T16:24:11.511+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:48394 #1 (1 connection now open)
 2018-08-23T16:24:11.513+0800 I NETWORK  [conn1] received client metadata from 127.0.0.1:48394 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.1" }, os: { type: "Linux", name: "CentOS Linux release 7.5.1804 (Core) ", architecture: "x86_64", version: "Kernel 3.10.0-862.el7.x86_64" } }

 xsell另一個視窗開啟:

[[email protected] ~]# cd /usr/local/mongodb/bin/
    [[email protected] bin]# ll
    總用量 208816
    -rwxr-xr-x. 1 root root  6906321 8月   4 01:43 bsondump
    -rwxr-xr-x. 1 root root     5792 8月   4 01:59 install_compass
    -rwxr-xr-x. 1 root root 38886336 8月   4 01:59 mongo
    -rwxr-xr-x. 1 root root 60972352 8月   4 01:59 mongod
    -rw-r--r--. 1 root root      151 8月  23 14:39 mongodb.conf
    -rwxr-xr-x. 1 root root 10255092 8月   4 01:43 mongodump
    -rwxr-xr-x. 1 root root  8245971 8月   4 01:43 mongoexport
    -rwxr-xr-x. 1 root root  8123365 8月   4 01:43 mongofiles
    -rwxr-xr-x. 1 root root  8404330 8月   4 01:43 mongoimport
    -rwxr-xr-x. 1 root root  9322864 8月   4 01:43 mongoreplay
    -rwxr-xr-x. 1 root root 11451628 8月   4 01:43 mongorestore
    -rwxr-xr-x. 1 root root 34621336 8月   4 01:59 mongos
    -rwxr-xr-x. 1 root root  8483293 8月   4 01:43 mongostat
    -rwxr-xr-x. 1 root root  8116571 8月   4 01:43 mongotop
    [[email protected] bin]# mongo
    MongoDB shell version v4.0.1
    connecting to: mongodb://127.0.0.1:27017
    MongoDB server version: 4.0.1
    Server has startup warnings: 
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] 
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] 
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
    2018-08-23T16:23:31.533+0800 I CONTROL  [initandlisten] 
    2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] 
    2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
    2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] 
    2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2018-08-23T16:23:31.534+0800 I CONTROL  [initandlisten] 
    > show dbs;
    admin   0.000GB
    config  0.000GB
    local   0.000GB
    test    0.000GB
    > 

三,PHP安裝MongoDB擴充套件的問題

1,問題

 執行/usr/local/php7/bin/pecl install mongodb  時出現:

 Configuring for:
 PHP Api Version:         20041225
 Zend Module Api No:      20060613
 Zend Extension Api No:   220060519
 Cannot find autoconf. Please check your autoconf installation and the
 $PHP_AUTOCONF environment variable. Then, rerun this script.

2,解決

 cd /usr/src
 wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
 tar -zvxf m4-1.4.9.tar.gz
 cd m4-1.4.9/
 ./configure && make && make install
 cd ../
 wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
 tar -zvxf autoconf-2.62.tar.gz
 cd autoconf-2.62/
 ./configure && make && make install

更簡單的辦法

 yum install m4
 yum install autoconf