linux下安装配置mongodb

一:下载

下载地址:

https://www.mongodb.org/

 

二:安装

2.1 解压

[root@ser6-51 download]# ls -ltr
total 71552
-rw-r--r-- 1 root root 32979820 Jun 15 10:01 mysql-5.6.20.tar.gz
drwxr-xr-x 4 root root     4096 Jun 16 13:39 mysql
-rw-r--r-- 1 root root 40280527 Jun 25 11:54 mongodb-linux-x86_64-3.0.4.tgz
[root@ser6-51 download]# tar -xvf mongodb-linux-x86_64-3.0.4.tgz 
mongodb-linux-x86_64-3.0.4/README
mongodb-linux-x86_64-3.0.4/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-3.0.4/GNU-AGPL-3.0
mongodb-linux-x86_64-3.0.4/bin/mongodump
mongodb-linux-x86_64-3.0.4/bin/mongorestore
mongodb-linux-x86_64-3.0.4/bin/mongoexport
mongodb-linux-x86_64-3.0.4/bin/mongoimport
mongodb-linux-x86_64-3.0.4/bin/mongostat
mongodb-linux-x86_64-3.0.4/bin/mongotop
mongodb-linux-x86_64-3.0.4/bin/bsondump
mongodb-linux-x86_64-3.0.4/bin/mongofiles
mongodb-linux-x86_64-3.0.4/bin/mongooplog
mongodb-linux-x86_64-3.0.4/bin/mongoperf
mongodb-linux-x86_64-3.0.4/bin/mongod
mongodb-linux-x86_64-3.0.4/bin/mongos
mongodb-linux-x86_64-3.0.4/bin/mongo
[root@ser6-51 download]# ls -ltr
total 71556
-rw-r--r-- 1 root root 32979820 Jun 15 10:01 mysql-5.6.20.tar.gz
drwxr-xr-x 4 root root     4096 Jun 16 13:39 mysql
-rw-r--r-- 1 root root 40280527 Jun 25 11:54 mongodb-linux-x86_64-3.0.4.tgz
drwxr-xr-x 3 root root     4096 Jun 25 11:54 mongodb-linux-x86_64-3.0.4
 


#为了方便管理,把安装文件移动到/usr/local

[root@ser6-51 download]# mv mongodb-linux-x86_64-3.0.4 /usr/local/mongodb

 

2.2 创建相关目录

为mongodb创建数据库存放的位置和日志文件

[root@ser6-51 mongodb]# cd /usr/local/mongodb

[root@ser6-51 mongodb]# mkdir data

[root@ser6-51 mongodb]# touch logs

[root@ser6-51 mongodb]# ls -ltr

total 72

-rw-r--r-- 1 1046 1046 22660 Jun 15 23:51 THIRD-PARTY-NOTICES

-rw-r--r-- 1 1046 1046  1359 Jun 15 23:51 README

-rw-r--r-- 1 1046 1046 34520 Jun 15 23:51 GNU-AGPL-3.0

drwxr-xr-x 2 root root  4096 Jun 25 11:54 bin

drwxr-xr-x 2 root root  4096 Jun 25 12:00 data

-rw-r--r-- 1 root root     0 Jun 25 12:00 logs

2.3 创建Linux用户

[root@ser6-51 init.d]# groupadd mongodb

[root@ser6-51 init.d]# useradd -g mongodb mongodb

[root@ser6-51 init.d]# chown -R mongodb:mongodb /usr/local/mongodb

 

2.4 配置PATH

#mongodb用户:

 

PATH末尾添加Mongodb 安装文件/bin路径:

 

[root@ser6-51 init.d]# su - mongodb 

 

[mongodb@ser6-51 ~]$ vi .bash_profile 

 

PATH=$PATH:$HOME/bin:/usr/local/mongodb/bin

[mongodb@ser6-51 ~]$ source .bash_profile

 

这样就可以不用进入到/usr/local/mongodb/bin路径下使用mongo命令了,直接输入mongo命令即可。

 

#root用户省略

三:启动mongodb

#验证是否安装成功

[mongodb@ser6-51 ~]$ mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs
about to fork child process, waiting until server is ready for connections.
forked process: 12135
child process started successfully, parent exiting
 
[root@ser6-51 bin]# mongo
MongoDB shell version: 3.0.4
connecting to: test
Server has startup warnings: 
2015-06-25T12:02:26.238+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-06-25T12:02:26.238+0800 I CONTROL  [initandlisten] 
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] 
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] 
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] 
> show dbs;
local  0.078GB

可以看到有Local数据库,说明已安装成功。


四:建配置文件

[mongodb@ser6-51 ~]$ cd /usr/local/mongodb

 

[mongodb@ser6-51 mongodb]$ vi mongod.cnf

添加:

 

logpath=/usr/local/mongodb/logs
logappend = true 
#fork and run in background
fork = true
#port = 27017
dbpath=/usr/local/mongodb/data
#location of pidfile
pidfilepath=/usr/local/mongodb/mongod.pid 

 

除了以mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs这种方式启动数据库外,还可以通过配置文件启动数据库:

 

之前打开数据库了,所以先关库:

可以使用db.shutdownServer()命令

 

> use admin
switched to db admin
> db.shutdownServer()
2015-06-25T15:15:57.839+0800 I NETWORK  DBClientCursor::init call() failed
server should be down...
2015-06-25T15:15:57.841+0800 I NETWORK  trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2015-06-25T15:15:57.842+0800 W NETWORK  Failed to connect to 127.0.0.1:27017, reason: errno:104 Connection reset by peer
2015-06-25T15:15:57.842+0800 I NETWORK  reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
  

#通过配置文件连接数据库

[mongodb@ser6-51 mongodb]$ mongod --config /usr/local/mongodb/mongod.cnf

about to fork child process, waiting until server is ready for connections.

forked process: 2059

child process started successfully, parent exiting

 

五:设置开机启动

[mongodb@ser6-51 mongodb]$ su -

Password: 

[root@ser6-51 ~]# vi /etc/rc.local

添加:

/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongod.cnf

 

#重启机器,检测是否生效

[root@ser6-51 ~]# reboot

--过一小会儿--

[root@ser6-51 ~]# ps -ef | grep mongod

root      1828     1  0 17:32 ?        00:00:00 /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs

root      1900  1624  0 17:33 pts/1    00:00:00 grep mongod

 

看到数据库已经起来了。

六:怎样使用以service方式启动数据库

前面启动数据库的方式略显繁琐,所以想用service mongodb start方式启动数据库。

 

默认service mongodb status会报错(由于该服务没有加入到service中

[root@ser6-51 bin]# service mongodb status

mongodb: unrecognized service

 

需要进行如下配置:

[root@ser6-51 ~]#vi /etc/init.d/mongod

添加:

 

#!/bin/sh
CONFIG=/usr/local/mongodb/mongod.cnf
PROGRAM=/usr/local/mongodb/bin/mongod
MONGOPID=`ps -ef | grep 'mongod --config' | grep -v grep | awk '{print $2}'`
 
test -x $PROGRAM || exit 0
 
case "$1" in
  start)
      echo "Starting MongoDB Server..."
      $PROGRAM --config $CONFIG &
  ;;
  stop)
      echo "Stopping MongoDB Server..."
      if [ ! -z "$MONGOPID" ]; then
          kill -15 $MONGOPID
      fi
  ;;
  status)
      if [  -z "$MONGOPID" ]; then
          echo "MongoDB is not running!"
      else
          echo "MongoDB is running!("$MONGOPID")"
      fi
  ;;
  restart)
     echo "Shutting down MongoDB Server..."
     if [ ! -z "$MONGOPID" ]; then
         kill -15 $MONGOPID
     fi
     echo "Starting MongoDB..."
     $PROGRAM --config $CONFIG &
  ;;
  *)
  log_success_msg "Usage:/etc/init.d/mongod {start|stop|status|restart}" 
exit 1
esac
exit 0          

 

#为该文件赋予执行权限

[root@ser6-51 ~]# ls -l /etc/init.d/mongod 

-rw-r--r--. 1 root root 529 Jun 25 17:33 /etc/init.d/mongod

[root@ser6-51 ~]# chmod 744 /etc/init.d/mongod

[root@ser6-51 ~]# ls -l /etc/init.d/mongod

-rwxr--r--. 1 root root 529 Jun 25 17:33 /etc/init.d/mongod

  

测试效果:

 

[root@ser6-51 mongodb]# service mongod stop

Stopping MongoDB Server

Terminated

 

Terminated

[root@ser6-51 mongodb]# service mongod start

Starting MongoDB Server

[root@ser6-51 mongodb]# about to fork child process, waiting until server is ready for connections.

forked process: 3632

child process started successfully, parent exiting

 

--开启用户认证,请参考MongoDB用户权限操作

--对应的shell安装脚本,请参考:mongodb 3.0.4 shell安装脚本

### 安装 MongoDB 的方法 #### 下载安装包 可以直接在 Linux 系统下下载 MongoDB 安装包并编写配置文件。使用 `wget` 加上安装包地址来获取软件包[^2]。 对于特定版本,比如 MongoDB 7.0,在麒麟系统或其他兼容的 Linux 发行版中,可以通过如下命令下载指定版本: ```bash wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-7.0.5.tgz ``` 此命令会从官方服务器下载适用于 Red Hat Enterprise Linux 8 或者兼容系统的二进制压缩包[^3]。 #### 解压与移动文件 下载完成后解压 tarball 文件,并将解压后的目录中的内容复制到目标位置,例如 `/data/mongodb/`: ```bash tar -zxvf mongodb-linux-x86_64-rhel80-7.0.5.tgz cp -fr mongodb-linux-x86_64-rhel80-7.0.5/* /data/mongodb/ ``` 这里 `-f` 参数表示强制覆盖现有文件;而 `-r` 则用于递归处理整个目录结构[^1]。 #### 配置环境变量 (可选) 为了方便调用 MongoDB 可执行程序,建议把其 bin 路径加入 PATH 中。编辑用户的 shell profile 文件(如 `.bashrc`),添加下面这行代码: ```bash export PATH=$PATH:/data/mongodb/bin ``` 使更改生效需重新加载该文件或重启终端窗口。 #### 创建数据存储路径及日志文件夹 确保有适当的数据存放地点以及日志记录的地方。通常情况下可以在根目录创建名为 `data/db` 和 `log` 的子文件夹作为默认设置。 ```bash mkdir -p /data/db mkdir -p /var/log/mongodb ``` #### 编辑配置文件 根据需求调整 MongoDB 的运行参数,保存在一个 .conf 文件里。以下是简单的例子: ```ini storage: dbPath: "/data/db" systemLog: destination: file path: "/var/log/mongodb/mongod.log" net: bindIp: "127.0.0.1" processManagement: fork: true # 后台模式启动 security: authorization: enabled # 如果启用认证则打开此项 ``` #### 初始化服务 完成上述准备工作之后就可以尝试首次启动 MongoDB 实例了。假设配置文件位于 `/etc/mongod.conf` ,那么可以这样操作: ```bash mongod -f /etc/mongod.conf & ``` & 符号使得进程以后台方式运行。如果一切正常,则说明已经成功部署了一个基本可用的服务实例[^4]。 #### 检查状态和服务管理 确认 MongoDB 是否正在监听端口并且能够响应请求。如果没有 netstat 工具的话,先通过 yum 来安装它: ```bash yum install -y net-tools ``` 接着利用 `ps aux | grep mongod` 查看守护线程是否存在,或者借助 `ss`, `lsof` 这样的工具查询开放端口号情况[^5]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雅冰石

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值