CentOS安装mongodb3.4以及php的mongodb扩展

 


 

安装mongodb

下载页:https://www.mongodb.com/download-center?jmp=nav#community

下载地址:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.3.tgz

 

cd /usr/local/src
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.3.tgz
cp mongodb-linux-x86_64-rhel70-3.4.3.tgz /usr/local
cd /usr/local
tar zxf mongodb-linux-x86_64-rhel70-3.4.3.tgz
mv mongodb-linux-x86_64-rhel70-3.4.3 mongodb
rm -rf mongodb-linux-x86_64-rhel70-3.4.3.tgz
cd /usr/local/mongodb
mkdir -p /db/mongodb
mkdir -p /data/log/mongodb

 

一、mongodb.conf

 

vim /usr/local/mongodb/conf/mongodb.conf

 

systemLog:
  destination: file
  logAppend: true
  path: /data/log/mongodb/mongodb.log
storage:
  dbPath: /db/mongodb
  journal:
    enabled: true

processManagement:
  fork: true 
#  pidFilePath: /var/run/mongodb/mongod.pid  
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27017
  bindIp: 127.0.0.1,192.168.0.200
#security:
#  authorization: enabled

 

bind_ip 监听哪些ip地址(网卡),首先这些ip是本机ip,服务器一般有三个网卡,本地回环网卡(127.0.0.1),内网网卡(192.168.0.200),外网网卡(51.52.53.54)

 

二、在/etc/profile里面增加环境变量

 

/usr/local/mongodb/bin
[root@localhost]# source /etc/profile
[root@localhost]# cat /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin:/usr/local/mysql/bin:/usr/local/mysql/lib:/usr/local/openssl/bin:/usr/local/php/bin:/usr/local/mongodb/bin

 

三、启动mongodb

 

[root@localhost]# /usr/local/mongodb/bin/mongod --config=/usr/local/mongodb/mongodb.conf

 

自启动、启动、停止、重启

将 /usr/local/mongodb/bin/mongod --config=/usr/local/mongodb/mongodb.conf 加入到/etc/rc.d/rc.local

chmod 0744 /etc/rc.d/rc.local #默认居然没有可执行权限

 

启动脚本(推荐)

 

vim /etc/init.d/mongod
#! /bin/bash

# chkconfig: 2345 90 91
# description: Start and Stop mongodb
# processname: mongod

MONGOPATH=/usr/local/mongodb
EXEC=${MONGOPATH}/bin/mongod
CONF="${MONGOPATH}/conf/mongodb.conf"
LOCKFILE=/var/lock/subsys/mongod
RETVAL=0
case "$1" in
        start)
                                 echo -n $"Starting mongod: "
                                 $EXEC -f $CONF
                                 RETVAL=$?
                                 echo
                                 [ $RETVAL -eq 0 ] && touch $LOCKFILE
                ;;
        stop)
                                 echo -n $"Stopping mongod: "
                                 $EXEC -f $CONF --shutdown
                                 RETVAL=$?
                                 echo
                                 [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
                ;;
        restart)
                ${0} stop
                ${0} start
                ;;
        *)
               echo "Usage: /etc/init.d/mongod {start|stop|restart}" >&2
               exit 1
esac

 

chmod 0744 /etc/init.d/mongod
chkconfig --add mongod
service mongod start|stop|restart

 

重启MongoDB

最好不要用kill -9来杀死mongodb进程,会产生mongod.lock文件,并且可能发生数据丢失
启动出错,把mongodb的data目录下的mongod.lock删掉,再重启

 

1、使用客户端mongo

 

[root@localhost]# /usr/local/mongodb/bin/mongo
> use admin
> db.shutdownServer()

 

2、直接 kill -2 PID 或者 kill -15 PID

 

http://leeyin.iteye.com/blog/993228

 

3、启动mongodb

 

[root@localhost]# /usr/local/mongodb/bin/mongod --config=/usr/local/mongodb/mongodb.conf

 

4、重启php-fpm

 

[root@localhost]# /etc/init.d/php-fpm restart

 

参考文档

https://docs.mongodb.com/master/tutorial/install-mongodb-on-red-hat/

https://yq.aliyun.com/ziliao/48410

 

限制内网某个IP可访问

环境

 

192.168.0.200 安装mongodb的服务器(本机)
192.168.1.250 另一台服务器需要连接mongodb

修改配置文件

 

绑定本机的局域网IP地址,网上大多文章说的是限制某个ip访问,其实是错误的,这里的bind_ip其实意思是指:监听本机哪些网卡IP

 

bind_ip = 127.0.0.1,192.168.0.200

 

防火墙

 

27017端口默认是不能访问的,我们需要指定某个内网ip可以访问本机27017端口,比如允许192.168.1.250这台服务器可以访问本机27017端口

 

[root@timophp.com ~]# firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.250" port protocol="tcp" port="27017" accept"
[root@timophp.com ~]# firewall-cmd --reload

 

windows上面是增加一个防火墙的入站规则:防火墙-高级设置-入站规则

 

安装php的mongodb扩展

方式一

 

我用这种方法不行

yum -y install php-pear php-devel
pecl install mongodb

 

方式二

 

下载:http://pecl.php.net/package/mongodb

http://pecl.php.net/get/mongodb-1.2.8.tgz

 

cd /usr/local/src
wget http://pecl.php.net/get/mongodb-1.2.8.tgz
tar zxf mongodb-1.2.8.tgz
cd mongodb-1.2.8
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

 

在php.ini里面加入extension=mongodb.so

 

reload或者重启php-fpm

/etc/init.d/php-fpm reload
#或者
/etc/init.d/php-fpm restart