訪問Mongodb官網https://www.mongodb.com/try/download/community,右側有選擇項,這里選擇centos7+tgz+4.2.9選擇項,
[root@guangzhou 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 正在決議主機 fastdl.mongodb.org (fastdl.mongodb.org)... 204.246.164.124, 204.246.164.81, 204.246.164.16, ... 正在連接 fastdl.mongodb.org (fastdl.mongodb.org)|204.246.164.124|:443... 已連接, 已發出 HTTP 請求,正在等待回應... 200 OK 長度:132776427 (127M) [application/gzip] 正在保存至: “mongodb-linux-x86_64-rhel70-4.2.9.tgz” 100%[========================================================================================>] 132,776,427 9.48MB/s 用時 14s 2020-09-29 15:01:33 (9.37 MB/s) - 已保存 “mongodb-linux-x86_64-rhel70-4.2.9.tgz” [132776427/132776427]) [root@guangzhou src]# tar zxvf mongodb-linux-x86_64-rhel70-4.2.9.tgz [root@guangzhou src]# mv mongodb-linux-x86_64-rhel70-4.2.9 /usr/local/mongodb [root@guangzhou src]# cd /usr/local/mongodb/ [root@guangzhou mongodb]# mkdir -p ./data/db [root@guangzhou mongodb]# mkdir log [root@guangzhou mongodb]# mv log logs [root@guangzhou mongodb]# touch ./logs/mongodb.log
新建組態檔:
[root@guangzhou mongodb]# vim mongodb.conf #埠號 port=27017 #db目錄 dbpath=/usr/local/mongodb/data/db #日志目錄 logpath=//usr/local/mongodb/logs/mongodb.log #后臺 fork=true #日志輸出 logappend=true #允許遠程IP連接 bind_ip=0.0.0.0
啟動mongodb:
[root@guangzhou 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
連接mongodb:
[root@guangzhou 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() --- #選擇資料庫 > use admin switched to db admin > show databases; admin 0.000GB config 0.000GB local 0.000GB
#創建root用戶 > db.createUser({user:"root",pwd:"freedom",roles:[{role:"userAdminAnyDatabase",db:"admin"},"readWriteAnyDatabase"]}) Successfully added user: { "user" : "root", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" }, "readWriteAnyDatabase" ] }
斷開重聯鏈接輸入驗證資訊:
[root@guangzhou mongodb]# ./bin/mongo > use admin switched to db admin > db.auth('root','freedom') 1
配置service檔案:
[root@guangzhou 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
重啟systemd服務,啟動mongodb:
[root@guangzhou mongodb]# chmod +x /usr/lib/systemd/system/mongod.service [root@guangzhou mongodb]# systemctl daemon-reload [root@guangzhou ~]# 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 9月 29 15:44:52 guangzhou systemd[1]: Starting mongodb... 9月 29 15:44:52 guangzhou mongod[25525]: about to fork child process, waiting until server is ready for connections. 9月 29 15:44:52 guangzhou mongod[25525]: forked process: 25527 9月 29 15:44:53 guangzhou mongod[25525]: child process started successfully, parent exiting 9月 29 15:44:53 guangzhou systemd[1]: Started mongodb. [root@guangzhou ~]# systemctl stop mongod [root@guangzhou ~]# systemctl status mongod ● mongod.service - mongodb Loaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; vendor preset: disabled) Active: inactive (dead) 9月 29 15:33:54 guangzhou systemd[1]: Unit mongod.service entered failed state. 9月 29 15:33:54 guangzhou systemd[1]: mongod.service failed. 9月 29 15:44:52 guangzhou systemd[1]: Starting mongodb... 9月 29 15:44:52 guangzhou mongod[25525]: about to fork child process, waiting until server is ready for connections. 9月 29 15:44:52 guangzhou mongod[25525]: forked process: 25527 9月 29 15:44:53 guangzhou mongod[25525]: child process started successfully, parent exiting 9月 29 15:44:53 guangzhou systemd[1]: Started mongodb. 9月 29 15:45:04 guangzhou systemd[1]: Stopping mongodb... 9月 29 15:45:04 guangzhou mongod[25625]: killing process with pid: 25527 9月 29 15:45:05 guangzhou systemd[1]: Stopped mongodb.
客戶端快捷方式創建:
[root@guangzhou mongodb]# ln -s /usr/local/mongodb/bin/mongo /usr/local/bin/mongo [root@guangzhou 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
開機啟動mongodb: systemctl enable mongod
手動啟動mongodb:systemctl start mongod
停止mongodb: systemctl stop mongod
查看運行狀態: systemctl status mongod
安裝Mongodb的php擴展:
[root@guangzhou src]# wget https://pecl.php.net/get/mongodb-1.7.4.tgz [root@guangzhou src]# tar -zxvf mongodb-1.7.4.tgz && cd mongodb-1.7.4/ [root@guangzhou mongodb-1.7.4]# /usr/local/php/bin/phpize [root@guangzhou mongodb-1.7.4]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@guangzhou mongodb-1.7.4]# make && make install [root@guangzhou mongodb-1.7.4]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/ 總用量 36028 -rwxr-xr-x 1 root root 602416 1月 30 2020 amqp.so -rwxr-xr-x 1 root root 377328 12月 1 2019 curl.so -rwxr-xr-x 1 root root 892016 12月 20 2019 event.so -rwxr-xr-x 1 root root 2754408 7月 26 19:12 intl.so -rwxr-xr-x 1 root root 5779432 10月 5 17:15 mongodb.so -rwxr-xr-x 1 root root 658504 12月 1 2019 mysqli.so -rwxr-xr-x 1 root root 4238372 11月 26 2019 opcache.a -rwxr-xr-x 1 root root 2292240 11月 26 2019 opcache.so -rwxr-xr-x 1 root root 2554184 12月 21 2019 redis.so -rwxr-xr-x 1 root root 15008632 12月 1 2019 swoole.so -rwxr-xr-x 1 root root 1712352 9月 23 18:27 swoole_tracker.so [root@guangzhou mongodb-1.7.4]# vim /etc/php.ini #新增配置 extension=mongodb.so [root@guangzhou 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 =https://www.cnblogs.com/wscsq789/archive/2020/09/29/> Master Value mongodb.debug => no value =https://www.cnblogs.com/wscsq789/archive/2020/09/29/> no value
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/173588.html
標籤:其他
