MongoDB 基础(2)

2021-10-19 16:11:01 浏览数 (1)

创建 mongo 实例

安装好 mongodb-org-server 后,系统中就已经创建好了如下目录或文件

代码语言:javascript复制
[root@h105 mongo]# rpm -ql mongodb-org-server
/etc/init.d/mongod
/etc/mongod.conf
/etc/sysconfig/mongod
/usr/bin/mongod
/usr/share/doc/mongodb-org-server-3.2.6
/usr/share/doc/mongodb-org-server-3.2.6/GNU-AGPL-3.0
/usr/share/doc/mongodb-org-server-3.2.6/MPL-2
/usr/share/doc/mongodb-org-server-3.2.6/README
/usr/share/doc/mongodb-org-server-3.2.6/THIRD-PARTY-NOTICES
/usr/share/man/man1/mongod.1
/var/lib/mongo
/var/log/mongodb
/var/log/mongodb/mongod.log
/var/run/mongodb
[root@h105 mongo]#

其中几个重要的目录如下:

File

Comment

/etc/init.d/mongod

启动脚本

/etc/mongod.conf

配置文件

/etc/sysconfig/mongod

配置文件

/usr/bin/mongod

服务程序

/var/log/mongodb/mongod.log

日志文件

/var/lib/mongo

数据文件目录

Tip: 可以直接使用 /etc/init.d/mongod startservice mongod start 来启动 mongo 服务

代码语言:javascript复制
[root@h105 ~]# /etc/init.d/mongod start 
Starting mongod: 
                                                           [  OK  ]
[root@h105 ~]# 

我们这里准备手动创建一个实例,而不是使用现成的,这样可以更加了解其动作机制


手动创建实例

创建必要目录结构

代码语言:javascript复制
[root@h105 ~]# mkdir mongo
[root@h105 ~]# cd mongo/
[root@h105 mongo]# ls
[root@h105 mongo]# mkdir data
[root@h105 mongo]# ls
data
[root@h105 mongo]# mkdir log
[root@h105 mongo]# mkdir conf
[root@h105 mongo]# mkdir bin
[root@h105 mongo]# ls
bin  conf  data  log
[root@h105 mongo]#

拷贝一份程序代码到 bin 目录中

代码语言:javascript复制
[root@h105 mongo]# cp /usr/bin/mongod bin
[root@h105 mongo]# 

创建一个配置文件

代码语言:javascript复制
[root@h105 mongo]# cd conf
[root@h105 conf]# vim mongod.conf 
[root@h105 conf]# cat mongod.conf 
port = 12345
dbpath = data
logpath = log/mongod.log
fork = true
[root@h105 conf]# 

启动服务

代码语言:javascript复制
[root@h105 mongo]# ./bin/mongod -f conf/mongod.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 3505
child process started successfully, parent exiting
[root@h105 mongo]#
[root@h105 mongo]# netstat  -ant | grep 1234
tcp        0      0 0.0.0.0:12345               0.0.0.0:*                   LISTEN      
[root@h105 mongo]# ll data/
total 152
-rw-r--r-- 1 root root 16384 May 18 16:57 collection-0-1422751514030254971.wt
drwxr-xr-x 2 root root  4096 May 18 16:57 diagnostic.data
-rw-r--r-- 1 root root 16384 May 18 16:57 index-1-1422751514030254971.wt
drwxr-xr-x 2 root root  4096 May 18 15:57 journal
-rw-r--r-- 1 root root 16384 May 18 16:57 _mdb_catalog.wt
-rw-r--r-- 1 root root     0 May 18 16:57 mongod.lock
-rw-r--r-- 1 root root 32768 May 18 16:57 sizeStorer.wt
-rw-r--r-- 1 root root    95 May 18 15:57 storage.bson
-rw-r--r-- 1 root root    46 May 18 15:57 WiredTiger
-rw-r--r-- 1 root root  4096 May 18 16:57 WiredTigerLAS.wt
-rw-r--r-- 1 root root    21 May 18 15:57 WiredTiger.lock
-rw-r--r-- 1 root root   915 May 18 16:57 WiredTiger.turtle
-rw-r--r-- 1 root root 45056 May 18 16:57 WiredTiger.wt
[root@h105 mongo]# ll log/
total 4
-rw-r--r-- 1 root root 3736 May 18 16:57 mongod.log
[root@h105 mongo]# 
[root@h105 mongo]# tail log/mongod.log 
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] 
2016-05-18T15:57:03.511 0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/root/mongo/data/diagnostic.data'
2016-05-18T15:57:03.512 0800 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-05-18T15:57:03.542 0800 I NETWORK  [initandlisten] waiting for connections on port 12345
[root@h105 mongo]#

0 人点赞