MongoDB 基础(3)

2021-10-19 19:40:47 浏览数 (1)

连接服务

使用mongo客户端来连接服务,mongo 客户端由 mongodb-org-shell 包提供

代码语言:javascript复制
[root@h105 ~]# rpm -ql mongodb-org-shell-3.2.6-1.el6.x86_64
/usr/bin/mongo
/usr/share/man/man1/mongo.1
[root@h105 ~]# 

拷贝一份到 bin 中

代码语言:javascript复制
[root@h105 mongo]# which mongo
/usr/bin/mongo
[root@h105 mongo]# cp /usr/bin/mongo bin/
[root@h105 mongo]# ll bin/
total 60696
-rwxr-xr-x 1 root root 22561172 May 18 16:54 mongo
-rwxr-xr-x 1 root root 39583898 May 18 15:55 mongod
[root@h105 mongo]# ./bin/mongo --help 
MongoDB shell version: 3.2.6
usage: ./bin/mongo [options] [db address] [file names (ending in .js)]
db address can be:
  foo                   foo database on local machine
  192.169.0.5/foo       foo database on 192.168.0.5 machine
  192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999
Options:
  --shell                             run the shell after executing files
  --nodb                              don't connect to mongod on startup - no 
                                      'db address' arg expected
  --norc                              will not run the ".mongorc.js" file on 
                                      start up
  --quiet                             be less chatty
  --port arg                          port to connect to
  --host arg                          server to connect to
  --eval arg                          evaluate javascript
  -h [ --help ]                       show this usage information
  --version                           show version information
  --verbose                           increase verbosity
  --ipv6                              enable IPv6 support (disabled by default)
  --disableJavaScriptJIT              disable the Javascript Just In Time 
                                      compiler
  --enableJavaScriptProtection        disable automatic JavaScript function 
                                      marshalling
  --ssl                               use SSL for all connections
  --sslCAFile arg                     Certificate Authority file for SSL
  --sslPEMKeyFile arg                 PEM certificate/key file for SSL
  --sslPEMKeyPassword arg             password for key in PEM file for SSL
  --sslCRLFile arg                    Certificate Revocation List file for SSL
  --sslAllowInvalidHostnames          allow connections to servers with 
                                      non-matching hostnames
  --sslAllowInvalidCertificates       allow connections to servers with invalid
                                      certificates
  --sslFIPSMode                       activate FIPS 140-2 mode at startup

Authentication Options:
  -u [ --username ] arg               username for authentication
  -p [ --password ] arg               password for authentication
  --authenticationDatabase arg        user source (defaults to dbname)
  --authenticationMechanism arg       authentication mechanism
  --gssapiServiceName arg (=mongodb)  Service name to use when authenticating 
                                      using GSSAPI/Kerberos
  --gssapiHostName arg                Remote host name to use for purpose of 
                                      GSSAPI/Kerberos authentication

file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
[root@h105 mongo]# 

进行连接

代码语言:javascript复制
[root@h105 mongo]# ./bin/mongo 127.0.0.1:12345/test
MongoDB shell version: 3.2.6
connecting to: 127.0.0.1:12345/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] 
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] 
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] 
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] 
>

停止服务

代码语言:javascript复制
[root@h105 mongo]# ./bin/mongo 127.0.0.1:12345/test
MongoDB shell version: 3.2.6
connecting to: 127.0.0.1:12345/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] 
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] 
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] 
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-05-18T15:57:03.510 0800 I CONTROL  [initandlisten] 
> db.shutdownServer()
shutdown command only works with the admin database; try 'use admin'
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
2016-05-18T16:57:34.674 0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:12345 (127.0.0.1) failed
2016-05-18T16:57:34.682 0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:12345, reason: errno:111 Connection refused
2016-05-18T16:57:34.682 0800 I NETWORK  [thread1] reconnect 127.0.0.1:12345 (127.0.0.1) failed failed 
> ^C
bye
[root@h105 mongo]# 

查看日志

代码语言:javascript复制
[root@h105 mongo]# tail -f log/mongod.log 
2016-05-18T16:57:34.670 0800 I CONTROL  [conn1] now exiting
2016-05-18T16:57:34.670 0800 I NETWORK  [conn1] shutdown: going to close listening sockets...
2016-05-18T16:57:34.670 0800 I NETWORK  [conn1] closing listening socket: 6
2016-05-18T16:57:34.670 0800 I NETWORK  [conn1] closing listening socket: 7
2016-05-18T16:57:34.670 0800 I NETWORK  [conn1] removing socket file: /tmp/mongodb-12345.sock
2016-05-18T16:57:34.670 0800 I NETWORK  [conn1] shutdown: going to flush diaglog...
2016-05-18T16:57:34.670 0800 I NETWORK  [conn1] shutdown: going to close sockets...
2016-05-18T16:57:34.670 0800 I STORAGE  [conn1] WiredTigerKVEngine shutting down
2016-05-18T16:57:34.823 0800 I STORAGE  [conn1] shutdown: removing fs lock...
2016-05-18T16:57:34.823 0800 I CONTROL  [conn1] dbexit:  rc: 0
^C
[root@h105 mongo]#

命令汇总

  • vim /etc/yum.repos.d/mongodb-org-3.2.repo
  • yum clean all
  • yum install -y mongodb-org
  • mongo --version
  • getenforce
  • grep -i '^SELINUX' /etc/sysconfig/selinux
  • rpm -ql mongodb-org-server
  • /etc/init.d/mongod start
  • mkdir mongo
  • cd mongo/
  • mkdir data
  • mkdir log
  • mkdir conf
  • mkdir bin
  • cp /usr/bin/mongod bin
  • cd conf
  • vim mongod.conf
  • ./bin/mongod -f conf/mongod.conf
  • netstat -ant | grep 1234
  • tail log/mongod.log
  • rpm -ql mongodb-org-shell-3.2.6-1.el6.x86_64
  • cp /usr/bin/mongo bin/
  • ./bin/mongo --help
  • ./bin/mongo 127.0.0.1:12345/test
  • tail -f log/mongod.log

原文地址

0 人点赞