Install MongoDB 3.0 on Red Hat Enterprise Linux(By YUM)
(本文参考官方文档:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/)
这里,通过YUM来安装MongoDB。
配置:/etc/yum.repos.d/xxx.repo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
[root@adamhuanlinux yum.repos.d]# pwd /etc/yum.repos.d [root@adamhuanlinux yum.repos.d]# ls mongodb-org-3.0.repo packagekit-media.repo redhat.repo [root@adamhuanlinux yum.repos.d]# cat mongodb-org-3.0.repo [mongodb-org-3.0] name=MongoDB Repository baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/ gpgcheck=0 enabled=1 [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# cat /etc/yum.conf [main] cachedir=/software/yum/$basearch/$releasever keepcache=1 debuglevel=2 logfile=/var/log/yum.log exactarch=1 obsoletes=1 gpgcheck=1 plugins=1 installonly_limit=3 # This is the default, if you make this bigger yum won't see if the metadata # is newer on the remote and so you'll "gain" the bandwidth of not having to # download the new metadata and "pay" for it by yum not having correct # information. # It is esp. important, to have correct metadata, for distributions like # Fedora which don't keep old packages around. If you don't like this checking # interupting your command line usage, it's much better to have something # manually check the metadata once an hour (yum-updatesd will do this). # metadata_expire=90m # PUT YOUR REPOS HERE OR IN separate files named file.repo # in /etc/yum.repos.d [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# yum makecache Loaded plugins: langpacks, product-id, subscription-manager mongodb-org-3.0 | 951 B 00:00:00 rhel-7-server-rpms | 3.7 kB 00:00:00 rhel-7-server-rpms/7Server/x86_64/filelists_db | 6.6 MB 00:01:29 (1/4): mongodb-org-3.0/7Server/primary | 6.2 kB 00:00:06 (2/4): mongodb-org-3.0/7Server/filelists | 1.6 kB 00:00:06 (3/4): mongodb-org-3.0/7Server/other | 1.4 kB 00:00:00 (4/4): rhel-7-server-rpms/7Server/x86_64/productid | 1.7 kB 00:00:07 mongodb-org-3.0 30/30 mongodb-org-3.0 30/30 mongodb-org-3.0 30/30 Metadata Cache Created [root@adamhuanlinux yum.repos.d]# |
安装:MongoDB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
[root@adamhuanlinux yum.repos.d]# yum list | grep --color mongodb mongodb-org.x86_64 3.0.3-1.el7 mongodb-org-3.0 mongodb-org-mongos.x86_64 3.0.3-1.el7 mongodb-org-3.0 mongodb-org-server.x86_64 3.0.3-1.el7 mongodb-org-3.0 mongodb-org-shell.x86_64 3.0.3-1.el7 mongodb-org-3.0 mongodb-org-tools.x86_64 3.0.3-1.el7 mongodb-org-3.0 [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# yum install -y mongodb-org.x86_64 mongodb-org-mongos.x86_64 mongodb-org-server.x86_64 mongodb-org-shell.x86_64 mongodb-org-tools.x86_64 Loaded plugins: langpacks, product-id, subscription-manager Resolving Dependencies --> Running transaction check ---> Package mongodb-org.x86_64 0:3.0.3-1.el7 will be installed ---> Package mongodb-org-mongos.x86_64 0:3.0.3-1.el7 will be installed ---> Package mongodb-org-server.x86_64 0:3.0.3-1.el7 will be installed ---> Package mongodb-org-shell.x86_64 0:3.0.3-1.el7 will be installed ---> Package mongodb-org-tools.x86_64 0:3.0.3-1.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =============================================================================================================================================================== Package Arch Version Repository Size =============================================================================================================================================================== Installing: mongodb-org x86_64 3.0.3-1.el7 mongodb-org-3.0 4.6 k mongodb-org-mongos x86_64 3.0.3-1.el7 mongodb-org-3.0 3.9 M mongodb-org-server x86_64 3.0.3-1.el7 mongodb-org-3.0 8.2 M mongodb-org-shell x86_64 3.0.3-1.el7 mongodb-org-3.0 4.1 M mongodb-org-tools x86_64 3.0.3-1.el7 mongodb-org-3.0 31 M Transaction Summary =============================================================================================================================================================== Install 5 Packages Total download size: 47 M Installed size: 148 M Downloading packages: (1/5): mongodb-org-3.0.3-1.el7.x86_64.rpm | 4.6 kB 00:00:06 (2/5): mongodb-org-mongos-3.0.3-1.el7.x86_64.rpm | 3.9 MB 00:00:31 (3/5): mongodb-org-shell-3.0.3-1.el7.x86_64.rpm | 4.1 MB 00:00:16 (4/5): mongodb-org-tools-3.0.3-1.el7.x86_64.rpm | 31 MB 00:00:58 (5/5): mongodb-org-server-3.0.3-1.el7.x86_64.rpm | 8.2 MB 00:02:48 --------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 278 kB/s | 47 MB 00:02:54 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mongodb-org-server-3.0.3-1.el7.x86_64 1/5 Installing : mongodb-org-shell-3.0.3-1.el7.x86_64 2/5 Installing : mongodb-org-mongos-3.0.3-1.el7.x86_64 3/5 Installing : mongodb-org-tools-3.0.3-1.el7.x86_64 4/5 Installing : mongodb-org-3.0.3-1.el7.x86_64 5/5 Verifying : mongodb-org-tools-3.0.3-1.el7.x86_64 1/5 Verifying : mongodb-org-mongos-3.0.3-1.el7.x86_64 2/5 Verifying : mongodb-org-shell-3.0.3-1.el7.x86_64 3/5 Verifying : mongodb-org-3.0.3-1.el7.x86_64 4/5 Verifying : mongodb-org-server-3.0.3-1.el7.x86_64 5/5 Installed: mongodb-org.x86_64 0:3.0.3-1.el7 mongodb-org-mongos.x86_64 0:3.0.3-1.el7 mongodb-org-server.x86_64 0:3.0.3-1.el7 mongodb-org-shell.x86_64 0:3.0.3-1.el7 mongodb-org-tools.x86_64 0:3.0.3-1.el7 Complete! [root@adamhuanlinux yum.repos.d]# |
启动MongoDB,并设为开机启动:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
[root@adamhuanlinux yum.repos.d]# ps -ef | grep --color mongod root 3695 3100 0 10:10 pts/0 00:00:00 grep --color=auto --color mongod [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# service mongod start Starting mongod (via systemctl): [ OK ] [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# ps -ef | grep --color mongod mongod 3722 1 0 10:11 ? 00:00:00 /usr/bin/mongod -f /etc/mongod.conf root 3737 3100 0 10:11 pts/0 00:00:00 grep --color=auto --color mongod [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# netstat -tupln | grep --color mongod tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 3722/mongod [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# chkconfig --list mongod Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. mongod 0:off 1:off 2:off 3:on 4:off 5:on 6:off [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# chkconfig mongod on [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# chkconfig --list mongod Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. mongod 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@adamhuanlinux yum.repos.d]# |
MongoDB的日志:mongod.log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
[root@adamhuanlinux yum.repos.d]# cat /var/log/mongodb/mongod.log 2015-06-02T10:11:06.214+0800 I CONTROL ***** SERVER RESTARTED ***** 2015-06-02T10:11:06.237+0800 I STORAGE [initandlisten] 2015-06-02T10:11:06.237+0800 I STORAGE [initandlisten] ** WARNING: Readahead for /var/lib/mongo is set to 4096KB 2015-06-02T10:11:06.237+0800 I STORAGE [initandlisten] ** We suggest setting it to 256KB (512 sectors) or less 2015-06-02T10:11:06.237+0800 I STORAGE [initandlisten] ** http://dochub.mongodb.org/core/readahead 2015-06-02T10:11:06.237+0800 I JOURNAL [initandlisten] journal dir=/var/lib/mongo/journal 2015-06-02T10:11:06.237+0800 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed 2015-06-02T10:11:08.862+0800 I JOURNAL [durability] Durability thread started 2015-06-02T10:11:08.863+0800 I JOURNAL [journal writer] Journal writer thread started 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] MongoDB starting : pid=3722 port=27017 dbpath=/var/lib/mongo 64-bit host=adamhuanlinux 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files. 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] db version v3.0.3 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] git version: b40106b36eecd1b4407eb1ad1af6bc60593c6105 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] build info: Linux ip-10-165-116-142 3.10.0-121.el7.x86_64 #1 SMP Tue Apr 8 10:48:19 EDT 2014 x86_64 BOOST_LIB_VERSION=1_49 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] allocator: tcmalloc 2015-06-02T10:11:08.888+0800 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/var/lib/mongo" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } } 2015-06-02T10:11:08.889+0800 I INDEX [initandlisten] allocating new ns file /var/lib/mongo/local.ns, filling with zeroes... 2015-06-02T10:11:09.247+0800 I STORAGE [FileAllocator] allocating new datafile /var/lib/mongo/local.0, filling with zeroes... 2015-06-02T10:11:09.247+0800 I STORAGE [FileAllocator] creating directory /var/lib/mongo/_tmp 2015-06-02T10:11:09.271+0800 I STORAGE [FileAllocator] done allocating datafile /var/lib/mongo/local.0, size: 64MB, took 0.012 secs 2015-06-02T10:11:09.300+0800 I NETWORK [initandlisten] waiting for connections on port 27017 [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# |
使用MongoDB:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
[root@adamhuanlinux yum.repos.d]# mongo --help MongoDB shell version: 3.0.3 usage: 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) --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@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# mongo --version MongoDB shell version: 3.0.3 [root@adamhuanlinux yum.repos.d]# [root@adamhuanlinux yum.repos.d]# mongo MongoDB shell version: 3.0.3 connecting to: test > help db.help() help on db methods db.mycoll.help() help on collection methods sh.help() sharding helpers rs.help() replica set helpers help admin administrative help help connect connecting to a db help help keys key shortcuts help misc misc things to know help mr mapreduce show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent system.profile entries with time >= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, 'global' is default use <db_name> set current database db.foo.find() list objects in collection foo db.foo.find( { a : 1 } ) list objects in foo where a == 1 it result of the last line evaluated; use to further iterate DBQuery.shellBatchSize = x set default number of items to display on shell exit quit the mongo shell > show dbs; local 0.078GB >> exit bye [root@adamhuanlinux yum.repos.d]# |
————————————————————————————————————
Done。
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/testing/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
官方下载链接:https://www.mongodb.com/download-center
启动后的MongoDB的样子:
[root@dg12c1 ~]# service mongod status
Redirecting to /bin/systemctl status mongod.service
● mongod.service – High-performance, schema-free document-oriented database
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2017-11-01 14:10:40 CST; 2s ago
Docs: https://docs.mongodb.org/manual
Process: 29471 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 29466 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 29464 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 29477 (mongod)
CGroup: /system.slice/mongod.service
└─29477 /usr/bin/mongod -f /etc/mongod.conf
Nov 01 14:10:40 dg12c1 systemd[1]: Starting High-performance, schema-free document-oriented database…
Nov 01 14:10:40 dg12c1 systemd[1]: Started High-performance, schema-free document-oriented database.
Nov 01 14:10:40 dg12c1 mongod[29473]: about to fork child process, waiting until server is ready for connections.
Nov 01 14:10:40 dg12c1 mongod[29473]: forked process: 29477
Nov 01 14:10:41 dg12c1 mongod[29473]: child process started successfully, parent exiting
[root@dg12c1 ~]#