mongodb是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
安装MongoDB
1、创建repo
vi /etc/yum.repos.d/mongodb-org-3.2.repo
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=0
enabled=1
2、安装MongoDB和相关工具
sudo yum install -y mongodb-org
3、启动MongoDB
4、验证MongoDB是否启动成功
cat /var/log/mongodb/mongod.log
查看是否有一句:[initandlisten] waiting for connections on port <port>
其中<port>是在/etc/mongod.conf中配置的,默认情况下是27017端口。
还有另一种方式:
# sudo 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; disabled; vendor preset: disabled)
Active: active (running) since 一 2016-09-12 09:50:16 CST; 15s ago
Process: 8787 ExecStart=/usr/bin/mongod $OPTIONS run (code=exited, status=0/SUCCESS)
Main PID: 8842 (mongod)
CGroup: /system.slice/mongod.service
└─8842 /usr/bin/mongod --quiet -f /etc/mongod.conf run
9月 12 09:50:13 192.168.1.155 systemd[1]: Starting High-performance, schema-free document-oriented database...
9月 12 09:50:14 192.168.1.155 mongod[8787]: about to fork child process, waiting until server is ready for connections.
9月 12 09:50:14 192.168.1.155 mongod[8787]: forked process: 8842
9月 12 09:50:16 192.168.1.155 mongod[8787]: child process started successfully, parent exiting
9月 12 09:50:16 192.168.1.155 systemd[1]: Started High-performance, schema-free document-oriented database.
会看到“Active: active (running)”,说明正在运行。
没有运行的情况:
# sudo 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; disabled; vendor preset: disabled)
Active: inactive (dead)
Active: inactive (dead)
5、使MongoDB开机自动启动
sudo chkconfig mongod on
6、停止MongoDB
sudo service mongod stop
7、重启MongoDB
sudo service mongod restart
进入Mongo命令行
# mongo
MongoDB shell version: 2.6.12
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2016-09-12T09:50:14.195+0800 [initandlisten]
2016-09-12T09:50:14.195+0800 [initandlisten] ** WARNING: Readahead for /var/lib/mongodb is set to 4096KB
2016-09-12T09:50:14.195+0800 [initandlisten] ** We suggest setting it to 256KB (512 sectors) or less
2016-09-12T09:50:14.195+0800 [initandlisten] ** http://dochub.mongodb.org/core/readahead
>
Linux下安装PHP的MongoDB扩展
因为是手动安装的MongoDB,所以也需要编译安装MongoDB扩展,步骤如下:
1)下载最新的PHP mongodb扩展源码,源码可以在http://pecl.php.net/package/mongo下载.
2)解压,进入安装目录
wget http://pecl.php.net/get/mongo-1.4.0.tgz
tar -zxvf mongo-1.4.0.tgz
cd mongo-1.4.0
3)进入文件夹后,首先运行phpize来编译扩展的环境
/usr/bin/phpize
PHP Api Version: 20121113
Zend Module Api No: 20121212
Zend Extension Api No: 220121212
4)运行后,我们运行./configure脚本来进行配置
`
./configure –with-php-config=/usr/local/php/bin/php-config && make && make install
–with-php-config 这个参数是告诉配置脚本 php-config 这个程序的路径
5)完成后,请编辑你[php](http://lib.csdn.net/base/php).ini文件增加一行
extension=mongo.so`
一般默认的编译php的ini文件/usr/local/php/etc/php.ini重启Apache/Nginx[或者/etc/init.d/php-fpm restart] 打开 phpinfo看到mongo模块,证明mongodb的php扩展安装成功。
OK ,至此你可以使用php来操作 MongoDB 了