EverKB

Knowledge Base Ever

在CentOS7上安裝MongoDB4

2019-11-15 11:30:33

標籤: CentOS, MongoDB, CentOS7, MongoDB4, Install MongoDB, 安裝MongoDB

編輯/etc/yum.repos.d/mongodb-enterprise.repo文件

[mongodb-enterprise]
name=MongoDB Enterprise Repository
baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/4.0/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

使用YUM安裝

yum -y install mongodb-enterprise

創建數據目錄和日誌目錄,並為目錄賦予權限

mkdir -p /var/lib/mongo
mkdir -p /var/log/mongodb
chown -R mongod:mongod /var/lib/mongo
chown -R mongod:mongod /var/log/mongodb

設置開機運行,並啟動服務

systemctl enable mongod
systemctl start mongod

創建用戶

[root@carbon ~]# mongo
MongoDB shell version v4.0.11
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("bf1a80fb-6a70-4bdb-86c7-8b3587400f87") }
MongoDB server version: 4.0.11
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/

MongoDB Enterprise > use admin
switched to db admin
MongoDB Enterprise > db.createUser(
...   {
...     user: "admin",
...     pwd: "admin",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
...   }

開啟授權登錄

MongoDB默認只在本機可以訪問,可以通過修改/etc/mongod.conf文件開啟授權登錄。

將bindIp修改為0.0.0.0,並且在#secruity行下加入授權的配置後重啟服務
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

#security:
security:
  authorization: enabled
systemctl restart mongod
此時已經可以用客戶端遠程連接了。客戶端配置如下:

IP:[MongoDB IP]
端口:27017
授權表:admin
用戶:admin
密碼:admin

驗證是否安裝成功,在mongo命令行中測試增刪查

[root@carbon ~]# mongo --port 27017 -u "admin" --authenticationDatabase "admin" -p
MongoDB shell version v4.0.11
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("dfe7aba3-9c0c-45cb-bf7b-457453c218ce") }
MongoDB server version: 4.0.11
MongoDB Enterprise > db.foo.insert( { x: 1, y: 1 } )
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.foo.find()
{ "_id" : ObjectId("5d45f6e21d57d1717b175746"), "x" : 1, "y" : 1 }
MongoDB Enterprise > db.foo.deleteOne({_id: ObjectId("5d45f6e21d57d1717b175746")})
{ "acknowledged" : true, "deletedCount" : 1 }
MongoDB Enterprise > db.foo.find()
MongoDB Enterprise > db.foo.drop()
true
MongoDB Enterprise > exit
bye

標籤

CentOS7 CentOS PHP Linux Python Python3 MySQL MariaDB YUM JavaScript mysql_native_password PHP7 CSS Redis MySQL8 caching_sha2_password HTML Bluetooth Failed to build these modules: _ctypes Minimal ISO substr Wi-Fi date sub INTERVAL PECL SQL Date Funcitons CodeIgniter font-size root Tag Cloud apache httpd dual network interfaces Laravel ss5 CURLFille horiz-align node.js ShadowsocksR vertical-align BarManager exec mb_substr PyCharm SUBDATE WebStorm date add IntelliJIDEA PEAR SOCKS5 Proxy Server Chrome find remi Tag Apache du jQuery PHP5.5 src cURL hide nginx Shadowsocks-Python V2Ray availability eth0 MariaDB10.4 PostgreSQL sub folder Web Server date Install MongoDB pdo_sqlsrv SOCKS5 File Upload mssql-tools register_shutdown_function Symfony Zend Framework ADDDATE DevExpress JetBrain php-fpm sqlsrv height network interface Shadowsocks-Libev unixODBC-devel Aura error_get_last Port 80 string Web Development data INDENTIFIED BY password policy requirements SOCKS Fedora Media Writer msodbcsql Redis5 SVN absolute Deepin jemalloc SQLAlchemy convert FuelPHP mysqlclient Shadowsocks toggle at-symbol Error Control Operator margin PHPUnit Framework ss5.passwd daemon process IIS password SOCKET vps center Fatal Errors MongoDB4 sudoers WiFi .htaccess DATE_SUB Phalcon SQL Server 2017 Compiling PHP frequency conflict set_error_handler TestCase apr Elasticsearch7.3 PHPUnit ss5.conf daemon HTTP particles.js Slim Vincent Garreau Failed to build these modules: _uuid MongoDB Sudo width DATE_ADD ip command pg_bha.conf SQL Server Compiling Apache Frameworks MySQL-python scrollbar Tcp BBR Apache2.4 Elasticsearch libpython3.7m.so.1.0 PHPStorm ss5-3.8.9 CURLOPT_POSTFIELDS NodeJs show VIM