八、设置开机启动
方法一、添加开机脚本
①在/etc/init.d/目录下添加mongod脚本,脚本内容如下:
#!/bin/bash
#chkconfig: 2345 80 90
#description: mongodb
start() {
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf
}
stop() {
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
②添加脚本执行权限
[root@xuegod63 ~]# chmod +x /etc/init.d/mongod
③设置开机启动
[root@xuegod63 ~]# chkconfig mongond --add
[root@xuegod63 ~]# chkconfig mongond on
[root@xuegod63 init.d]# service mongod restart
killing process with pid: 15302
about to fork child process, waiting until server is ready for connections.
forked process: 15430
child process started successfully, parent exiting
[root@xuegod63 init.d]# ps -efl |grep mongod |grep -v grep
1 S root 15525 1 2 80 0 - 80141 poll_s 19:47 ? 00:00:01 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf
[root@xuegod63 init.d]# netstat -tunlp |grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 15724/mongod
方法二、将启动进程添加到/etc/rc.local中
[root@xuegod63 ~]# echo "/usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data --logpath=/usr/local/mongodb/logs/mongodb.log --logappend --port 27017 --fork" >> /etc/rc.local
[root@xuegod63 ~]# service mongod stop
killing process with pid: 16781
[root@xuegod63 ~]# ps -efl |grep mongod |grep -v grep
[root@xuegod63 ~]# /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data --logpath=/usr/local/mongodb/logs/mongodb.log --logappend --port 27017 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 16897
child process started successfully, parent exiting
//不需要密码的登录方式
[root@xuegod63 ~]# /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data --logpath=/usr/local/mongodb/logs/mongodb.log --logappend --port=27017 --fork --auth
//需要权限的登录方式, 用户连接需要用户名和密码
[root@xuegod63 ~]# ps -efl |grep mongod |grep -v grep
1 S root 16897 1 2 80 0 - 98274 poll_s 20:26 ? 00:00:00 /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data --logpath=/usr/local/mongodb/logs/mongodb.log --logappend --port 27017 --fork
[root@xuegod63 ~]# netstat -tunlp |grep mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 16897/mongod
九、设置防火墙方便局域网内的其它服务器访问
[root@xuegod63 ~]# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
[root@xuegod63 ~]# /etc/init.d/iptables save
[root@xuegod63 ~]# /etc/init.d/iptables restart
十、连接测试
[root@xuegod63 ~]# mongo
MongoDB shell version: 3.2.4
connecting to: test
Server has startup warnings:
2016-03-30T05:09:28.032+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-03-30T05:09:28.032+0800 I CONTROL [initandlisten]
2016-03-30T05:09:28.032+0800 I CONTROL [initandlisten]
2016-03-30T05:09:28.032+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-03-30T05:09:28.032+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-03-30T05:09:28.032+0800 I CONTROL [initandlisten]
2016-03-30T05:09:28.032+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-03-30T05:09:28.032+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-03-30T05:09:28.032+0800 I CONTROL [initandlisten]
>
#查看当前数据库
> db
test
#切到数据库 mydb
> use mydb
switched to db mydb
> db
mydb
#用javascript创建两个文档
> j = { name : "mongo" }
{ "name" : "mongo" }
> k = { x : 3 }
{ "x" : 3 }
#创建mydb数据库和testdata collection
> db.testdata.insert(j)
WriteResult({ "nInserted" : 1 })
> db.testdata.insert(k);
WriteResult({ "nInserted" : 1 })
> show collections
system.indexes
testdata
> db.testdata.find()
{ "_id" : ObjectId("54ddd717d692cfe0bd20d983"), "name" : "mongo" }
{ "_id" : ObjectId("54ddd728d692cfe0bd20d984"), "x" : 3 }
> show dbs
admin (empty)
local 0.078GB
mydb 0.078GB
> use hahadb
switched to db hahadb
> show dbs
admin (empty)
local 0.078GB
mydb 0.078GB
> db
hahadb
> j = { name : "mongo" }
{ "name" : "mongo" }
> show dbs
admin (empty)
local 0.078GB
mydb 0.078GB
> db.testdata.insert(j)
WriteResult({ "nInserted" : 1 })
> show dbs
admin (empty)
hahadb 0.078GB
local 0.078GB
mydb 0.078GB
> use mydb
switched to db mydb
> show collections
system.indexes
testdata
> db.testdata.insert(j)
WriteResult({ "nInserted" : 1 })
> db.testdata.find()
{ "_id" : ObjectId("54ddd717d692cfe0bd20d983"), "name" : "mongo" }
{ "_id" : ObjectId("54ddd728d692cfe0bd20d984"), "x" : 3 }
{ "_id" : ObjectId("54ddda64d692cfe0bd20d986"), "name" : "mongo" }
> db.testdata.insert(k)
WriteResult({ "nInserted" : 1 })
> db.testdata.find()
{ "_id" : ObjectId("54ddd717d692cfe0bd20d983"), "name" : "mongo" }
{ "_id" : ObjectId("54ddd728d692cfe0bd20d984"), "x" : 3 }
{ "_id" : ObjectId("54ddda64d692cfe0bd20d986"), "name" : "mongo" }
{ "_id" : ObjectId("54ddda7dd692cfe0bd20d987"), "x" : 3 }
> db.testdata.find({x:3})
{ "_id" : ObjectId("54ddd728d692cfe0bd20d984"), "x" : 3 }
{ "_id" : ObjectId("54ddda7dd692cfe0bd20d987"), "x" : 3 }
> exit;
[root@xuegod63 ~]#
参考:
docs.mongodb.org/manual/tutorial/install-mongodb-on-linux/
十一、图形化管理(类似phpmyadmin)
http://code.google.com/p/rock-php/wiki/rock_mongo_zh
下载解压修改config.php为对应的地址及端口,使用admin:admin访问即可。
十二、备份及恢复现有数据库(数据库名为blog备份到/backup目录下)
[root@xuegod63 ~]# /usr/local/mongodb/bin/mongodump -h 127.0.0.1 -d blog -o /backup/blog.dmp
[root@xuegod63 ~]# /usr/local/mongodb/bin/mongorestore -h 127.0.0.1 -d blog /backup/ddc.dmp
十三、php mongo扩展安装:
php版本5.2.17编译安装,安装路径/usr/local/php
13.1、下载mongodb扩展源码
源码在http://pecl.php.net/package/mongo下载到
[root@xuegod63 ~]# wget http://pecl.php.net/get/mongo-1.2.2.tgz
[root@xuegod63 ~]# tar -zxvf mongo-1.2.2.tgz
[root@xuegod63 ~]# cd mongo-1.2.2
13.2、执行phpize来准备编译扩展的环境
[root@xuegod63 ~]# /usr/local/php/bin/phpize
运行后执行结果如下:
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
13.3、配置php mongo扩展
[root@xuegod63 ~]# ./configure --with-php-config=/usr/local/php/bin/php-config
--with-php-config这个参数是告诉配置脚本php-config这个程序的路径,php-config的介绍在这里
上面命令在正确配置的环境下运行结果如下:
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
13.4、编译安装扩展
[root@xuegod63 ~]# make && make install
正确编译执行结果如下:
Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
13.5、编辑php.ini文件
[root@xuegod63 ~]# vim /usr/local/php/etc/php.ini
extension=mongo.so
一般默认的编译php的ini文件在/usr/local/php/etc/php.ini
13.6、重启服务验证安装结果
测试:
[root@xuegod63 ~]# cd /var/www/html
[root@xuegod63 ~]# vim index.php #编辑输入下面内容
<?php
phpinfo();
?>
:wq! #保存退出
重启服务:
[root@xuegod63 ~]# service httpd restart
or
[root@xuegod63 ~]# service php-fpm restart
在客户端浏览器输入服务器IP地址,如果可以看到mongo相关的配置信息!那么mongodb的扩展就安装成功了。
http://localhost/index.php

至此,所有安装全部完成。