PHP的mongodb扩展
1)下载最新的PHP MongoDB扩展源码,源码可以在
http://pecl.php.net/package/mongo下载.
2)解压,进入安装目录
wget http://pecl.php.net/get/mongo-1.5.4.tgz
tar -zxvf mongo-1.5.4.tgz
cd mongo-1.5.4
3)进入文件夹后,首先运行phpize来编译扩展的环境
[root@localhost mongo-1.5.4]# phpize
Configuring for:
PHP Api Version: 20121113
Zend Module Api No: 20121212
Zend Extension Api No: 220121212
3) cd
4)运行后,我们运行./configure脚本来进行配置
./configure --with-php-config=/usr/local/php/bin/php-config && make && make install
–with-php-config 这个参数是告诉配置脚本 php-config 这个程序的路径
5)完成后,请编辑你php.ini文件增加一行 /usr/local/php/etc/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扩展安装成功。
二.PHP的mongo的扩展
1、安装mongo扩展
wget http://pecl.php.net/get/mongo-1.6.11.tgz
tar zxvf mongo-1.6.11.tgz
cd mongo-1.6.11
/usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config
make && make install
32位系统在 Installing shared extensions: /usr/lib/php/modules/
64位系统在 Installing shared extensions: /usr/lib64/php/modules/
2、php加载扩展
vi /etc/php.ini
extension_dir = "/usr/lib64/php/modules"
extension="mongo.so"
:wq! 保存退出
3、重新加载php-fpm
service php-fpm reload
## Mongodb副本集



## Mongodb副本集搭建
以mongo1为主库,mongo2,mongo3为从库进行集合搭建。设定mongo1中原本有数据
在mongo1建测试数据
进入mongo1的数据库
并在数据库中新建了一个集合test并在其中插入一条数据
[root@localhost ~]# mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
Server has startup warnings:
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten]
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten]
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten]
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten]
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-12-17T16:21:29.868-0800 I CONTROL [initandlisten]
> use test
switched to db test
> db.test.insert({"name":"super sbc"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
local 0.000GB
test 0.000GB
删除mongo2,mongo3中的所有数据库
分别进入mongo2,mongo3
删除所有数据库(包括admin和local)
[root@localhost ~]# mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
Server has startup warnings:
2019-12-17T16:57:33.260-0800 I CONTROL [initandlisten]
2019-12-17T16:57:33.260-0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-12-17T16:57:33.260-0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-12-17T16:57:33.260-0800 I CONTROL [initandlisten]
2019-12-17T16:57:33.260-0800 I CONTROL [initandlisten]
2019-12-17T16:57:33.260-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-12-17T16:57:33.260-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-12-17T16:57:33.260-0800 I CONTROL [initandlisten]
2019-12-17T16:57:33.260-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-12-17T16:57:33.260-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-12-17T16:57:33.260-0800 I CONTROL [initandlisten]
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
2019-12-17T16:50:42.453-0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2019-12-17T16:50:42.453-0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2019-12-17T16:50:42.453-0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed
> exit
bye
创建集合
分别在mongo1,mongo2和mongo3上启动命名为sbc的mongod服务
mongo1:
[root@localhost ~]# mongod --replSet sbc -f /etc/mongod.conf --fork
about to fork child process, waiting until server is ready for connections.
forked process: 73721
child process started successfully, parent exiting
mongo2:
[root@localhost ~]# mongod --replSet sbc -f /etc/mongod.conf --fork
about to fork child process, waiting until server is ready for connections.
forked process: 96907
child process started successfully, parent exiting
mongo3:
[root@localhost ~]# mongod --replSet sbc -f /etc/mongod.conf --fork
about to fork child process, waiting until server is ready for connections.
forked process: 96105
child process started successfully, parent exiting
配置集合
在mongo1端进行集合配置
config文件配置
进入mongo1的mongodb数据库服务,声明配置文件
[root@localhost ~]# mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
Server has startup warnings:
2019-12-17T17:36:34.302-0800 I CONTROL [initandlisten]
2019-12-17T17:36:34.302-0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-12-17T17:36:34.302-0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-12-17T17:36:34.302-0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-12-17T17:36:34.302-0800 I CONTROL [initandlisten]
2019-12-17T17:36:34.302-0800 I CONTROL [initandlisten]
2019-12-17T17:34.303-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-12-17T17:36:34.303-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-12-17T17:36:34.303-0800 I CONTROL [initandlisten]
2019-12-17T17:36:34.303-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-12-17T17:36:34.303-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-12-17T17:36:34.303-0800 I CONTROL [initandlisten]
> config = {
... "_id" : "sbc",
... "members" : [
... {"_id" : 0, "host" : "192.168.70.128:27017"},
... {"_id" : 1, "host" : "192.168.70.129:27017"},
... {"_id" : 2, "host" : "192.168.70.130:27017"}
... ]
... }
{
"_id" : "sbc",
"members" : [
{
"_id" : 0,
"host" : "192.168.70.128:27017"
},
{
"_id" : 1,
"host" : "192.168.70.129:27017"
},
{
"_id" : 2,
"host" : "192.168.70.130:27017"
}
]
}
初始化集群
在mongo1服务中初始化集群
> rs.initiate(config)
{ "ok" : 1 }
检查集群状态
在mongo1服务中查看集群状态
sbc:OTHER> rs.status()
{
"set" : "sbc",
"date" : ISODate("2019-12-17T18:47:25.901Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1511164036, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1511164036, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1511164036, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.70.128:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 653,
"optime" : {
"ts" : Timestamp(1511164036, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-12-17T18:47:16Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1511163995, 1),
"electionDate" : ISODate("2019-12-17T18:46:35Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.70.129:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 60,
"optime" : {
"ts" : Timestamp(1511164036, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1511164036, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-12-17T18:47:16Z"),
"optimeDurableDate" : ISODate("2019-12-17T18:47:16Z"),
"lastHeartbeat" : ISODate("2019-12-17T18:47:25.220Z"),
"lastHeartbeatRecv" : ISODate("2019-12-17T18:47:24.519Z"),
"pingMs" : NumberLong(1),
"syncingTo" : "192.168.70.128:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.70.130:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 60,
"optime" : {
"ts" : Timestamp(1511164036, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1511164036, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-12-17T18:47:16Z"),
"optimeDurableDate" : ISODate("2019-12-17T18:47:16Z"),
"lastHeartbeat" : ISODate("2019-12-17T18:47:25.207Z"),
"lastHeartbeatRecv" : ISODate("2019-12-17T18:47:24.448Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.70.128:27017",
"configVersion" : 1
}
],
"ok" : 1
}
可以看出mongo1为主节点,mongo2和mongo3为从节点,集群状态正常,搭建完成

