標籤:客戶 pat start nali core query access recv conf
在linux下通過apt-get install mongo的方式一鍵式安裝mongo
安裝後mongo的設定檔位於/etc/mongodb.conf. 裡面有mongo的各項配置,檔案內容如下:重要的主要有下面幾項:
1 dbpath:存放資料庫檔案的地方
2 logpath: 存放log的地方
3 bind_ip = 127.0.0.1。安裝好之後預設只允許本地訪問。限制訪問的IP為127.0.0.1。如果要允許所有的地址訪問。需要將此行注釋掉
4 port:訪問的連接埠號碼
5 auth=true (添加帳號,密碼認證)
[email protected]:/home/zhf# cat /etc/mongodb.conf
# mongodb.conf
# Where to store the data.
dbpath=/var/lib/mongodb
#where to log
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 127.0.0.1
#port = 27017
# Enable journaling, http://www.mongodb.org/display/DOCS/Journaling
journal=true
# Enables periodic logging of CPU utilization and I/O wait
#cpu = true
# Turn on/off security. Off is currently the default
#noauth = true
#auth = true
# Verbose logging output.
#verbose = true
# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true
# Enable db quota management
#quota = true
# Set oplogging level where n is
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#oplog = 0
# Diagnostic/debugging option
#nocursors = true
# Ignore query hints
#nohints = true
# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface = true
# Turns off server-side scripting. This will result in greatly limited
# functionality
#noscripting = true
# Turns off table scans. Any query that would do a table scan fails.
#notablescan = true
# Disable data file preallocation.
#noprealloc = true
# Specify .ns file size for new databases.
# nssize = <size>
# Accout token for Mongo monitoring server.
#mms-token = <token>
# Server name for Mongo monitoring server.
#mms-name = <server-name>
# Ping interval for Mongo monitoring server.
#mms-interval = <seconds>
# Replication Options
# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
#master = true
#source = slave.example.com
# Address of a server to pair with.
#pairwith = <server:port>
# Address of arbiter server.
#arbiter = <server:port>
# Automatically resync if slave data is stale
#autoresync
# Custom size for replication operation log.
#oplogSize = <MB>
# Size limit for in-memory storage of op ids.
#opIdMem = <bytes>
# SSL options
# Enable SSL on normal ports
#sslOnNormalPorts = true
# SSL Key file and password
#sslPEMKeyFile = /etc/ssl/mongodb.pem
#sslPEMKeyPassword = pass
安裝後之後輸入mongo命令進入shell介面
[email protected]:/home/zhf# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Server has startup warnings:
2017-12-12T20:16:00.856+0800 I STORAGE [initandlisten]
2017-12-12T20:16:00.856+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-12-12T20:16:00.856+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-12-12T20:16:04.012+0800 I CONTROL [initandlisten]
2017-12-12T20:16:04.012+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-12-12T20:16:04.012+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-12-12T20:16:04.012+0800 I CONTROL [initandlisten]
可以通過db.system.users.find()來查詢所有的使用者
添加使用者名稱和帳號
> db.createUser({user:‘zhf‘,pwd:‘123‘,roles:[‘userAdminAnyDatabase‘]})
Successfully added user: { "user" : "zhf", "roles" : [ "userAdminAnyDatabase" ]
設定遠端連線:
設定檔修改如下:注釋掉bind_ip,
#bind_ip = 127.0.0.1
port = 27017
添加路由開發27017連接埠
[email protected]:/home/zhf# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 27017 -j ACCEPT
啟動mong服務。
[email protected]:/var/lib/mongodb# mongod --dbpath /var/lib/mongodb
2017-12-12T22:39:13.060+0800 I CONTROL [initandlisten] MongoDB starting : pid=16691 port=27017 dbpath=/var/lib/mongodb 64-bit host=zhf-maple
2017-12-12T22:39:13.061+0800 I CONTROL [initandlisten] db version v3.4.7
2017-12-12T22:39:13.061+0800 I CONTROL [initandlisten] git version: cf38c1b8a0a8dca4a11737581beafef4fe120bcd
2017-12-12T22:39:13.061+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
2017-12-12T22:39:13.061+0800 I CONTROL [initandlisten] allocator: tcmalloc
2017-12-12T22:39:13.061+0800 I CONTROL [initandlisten] modules: none
2017-12-12T22:39:13.061+0800 I CONTROL [initandlisten] build environment:
2017-12-12T22:39:13.061+0800 I CONTROL [initandlisten] distarch: x86_64
2017-12-12T22:39:13.061+0800 I CONTROL [initandlisten] target_arch: x86_64
2017-12-12T22:39:13.061+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "/var/lib/mongodb" } }
2017-12-12T22:39:13.079+0800 E NETWORK [initandlisten] listen(): bind() failed Address already in use for socket: 0.0.0.0:27017
2017-12-12T22:39:13.079+0800 E NETWORK [initandlisten] addr already in use
2017-12-12T22:39:13.079+0800 E NETWORK [initandlisten] Failed to set up sockets during startup.
2017-12-12T22:39:13.079+0800 E STORAGE [initandlisten] Failed to set up listener: InternalError: Failed to set up sockets
2017-12-12T22:39:13.079+0800 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-12-12T22:39:13.079+0800 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-12-12T22:39:13.079+0800 I CONTROL [initandlisten] now exiting
2017-12-12T22:39:13.079+0800 I CONTROL [initandlisten] shutting down with code:48
這裡提示連接埠和地址已經被使用。通過netstat命令可以查看到確實有一個
[email protected]:/var/lib/mongodb# netstat -anp|more
啟用Internet串連 (伺服器和已建立串連的)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 749/mongod
殺掉正在啟動並執行進程:kill -9 749並再次重啟服務:
[email protected]:/var/lib/mongodb# mongod --dbpath /var/lib/mongodb
2017-12-12T22:41:25.966+0800 I CONTROL [initandlisten] MongoDB starting : pid=16780 port=27017 dbpath=/var/lib/mongodb 64-bit host=zhf-maple
2017-12-12T22:41:25.966+0800 I CONTROL [initandlisten] db version v3.4.7
2017-12-12T22:41:25.966+0800 I CONTROL [initandlisten] git version: cf38c1b8a0a8dca4a11737581beafef4fe120bcd
2017-12-12T22:41:25.966+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
2017-12-12T22:41:25.966+0800 I CONTROL [initandlisten] allocator: tcmalloc
2017-12-12T22:41:25.966+0800 I CONTROL [initandlisten] modules: none
2017-12-12T22:41:25.966+0800 I CONTROL [initandlisten] build environment:
2017-12-12T22:41:25.966+0800 I CONTROL [initandlisten] distarch: x86_64
2017-12-12T22:41:25.966+0800 I CONTROL [initandlisten] target_arch: x86_64
2017-12-12T22:41:25.966+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "/var/lib/mongodb" } }
2017-12-12T22:41:25.984+0800 I - [initandlisten] Detected data files in /var/lib/mongodb created by the ‘wiredTiger‘ storage engine, so setting the active storage engine to ‘wiredTiger‘.
2017-12-12T22:41:25.984+0800 I STORAGE [initandlisten]
2017-12-12T22:41:25.984+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-12-12T22:41:25.984+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-12-12T22:41:25.984+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3417M,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),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-12-12T22:41:27.096+0800 I CONTROL [initandlisten]
2017-12-12T22:41:27.096+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-12-12T22:41:27.096+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-12-12T22:41:27.096+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-12-12T22:41:27.096+0800 I CONTROL [initandlisten]
2017-12-12T22:41:27.103+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory ‘/var/lib/mongodb/diagnostic.data‘
2017-12-12T22:41:27.104+0800 I NETWORK [thread1] waiting for connections on port 27017
2017-12-12T22:41:28.028+0800 I FTDC [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK
2017-12-12T22:41:50.776+0800 I NETWORK [thread1] connection accepted from 192.168.0.11:35718 #1 (1 connection now open)
2017-12-12T22:46:10.454+0800 I - [conn1] end connection 192.168.0.11:35718 (1 connection now open)
2017-12-12T22:46:54.889+0800 I NETWORK [thread1] connection accepted from 192.168.0.11:35734 #2 (1 connection now open)
代表格服務開啟成功。
在用戶端上通過mongo 192.168.0.12:27017/admin 訪問伺服器的admin資料庫
[email protected]:~# mongo 192.168.0.12:27017/admin
MongoDB shell version: 2.6.10
connecting to: 192.168.0.12:27017/admin
Server has startup warnings:
2017-12-12T22:41:25.984+0800 I STORAGE [initandlisten]
2017-12-12T22:41:25.984+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-12-12T22:41:25.984+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-12-12T22:41:27.096+0800 I CONTROL [initandlisten]
2017-12-12T22:41:27.096+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-12-12T22:41:27.096+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-12-12T22:41:27.096+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-12-12T22:41:27.096+0800 I CONTROL [initandlisten]
>
關閉伺服器。主要有兩種方法:
1 輸入Ctrl+C關閉。這種關閉方式會等待當前進行中的的操作完成,所以依然是乾淨的關閉方式。
2 登入資料庫。然後輸入下面的命令進行關閉
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
mongodb學習之:mongo安裝以及遠端存取