项目中要使用mongodb,记录下采坑记录。
先说下本人的环境(安装不讲环境就是耍流氓)
环境:
PHP版本:7.0
系统环境:ubantu14.04
一、mongodb的安装。
按照网上的方法,安装了mongodb3.4,具体安装教程,由于之前是写在新浪的。忘了原文出处,此处请原作者谅解
1、导入包管理系统使用的公钥
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
2、为MongoDB创建一个列表文件
Ubuntu 12.04
echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Ubuntu 14.04
echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Ubuntu 16.04
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
3、重新加载本地包数据库
sudo apt-get update
4、安装最新的稳定版本的MongoDB
sudo apt-get install -y mongodb-org
5、启动MongoDB
sudo service mongod start
6、关闭MongoDB
sudo service mongod stop
7、重启MongoDB
sudo service mongod restart
8、卸载MongoDB
-
sudo service mongod stop
-
sudo apt-get purge mongodb-org*
-
sudo rm -r /var/log/mongodb
-
sudo rm -r /var/lib/mongodb
9、配置文件
-
dbpath
-
/var/lib/mongodb
-
logpath
-
/var/log/mongodb
-
config path
-
/var/etc/mongo.conf
这里有个小坑:就是安装完后使用mongo命令时,提示如下:
2018-08-02T17:14:00.218+0800 I STORAGE [initandlisten]
2018-08-02T17:14:00.218+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-08-02T17:14:00.218+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-08-02T17:14:00.522+0800 I CONTROL [initandlisten]
2018-08-02T17:14:00.522+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-02T17:14:00.522+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-08-02T17:14:00.522+0800 I CONTROL [initandlisten]
2018-08-02T17:14:00.523+0800 I CONTROL [initandlisten]
2018-08-02T17:14:00.523+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-08-02T17:14:00.523+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-08-02T17:14:00.523+0800 I CONTROL [initandlisten]
2018-08-02T17:14:00.523+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-08-02T17:14:00.523+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-08-02T17:14:00.523+0800 I CONTROL [initandlisten]
这个问题是由于没有初始化造成的,按照以下步骤解决:
https://docs.mongodb.com/guides/server/install/
二、php的扩展的安装
这里有坑,按照网上的安装了最新的mongodb扩展后,在php-fpm中的php.ini里添加了扩展后,
phpinfo也看到了mongodb扩展,提示如下:
PHP warning
include(MongoClient.php): failed to open stream: No such file or directory
但是按照目前网上的都使用不了,后来才发现是新的mongdb使用方式已经改变:
具体使用参考如下:
http://php.net/manual/zh/mongodb-driver-manager.construct.php
也就是不能再使用 MongoClient的方式来连接了,而是