Home > Backend Development > PHP7 > How to install mongo extension in php7.0

How to install mongo extension in php7.0

藏色散人
Release: 2023-02-18 10:36:01
Original
2133 people have browsed it

php7.0安装mongo扩展的方法:1、创建mongodb用户组和用户;2、下载mongodb源码包,并将源码包放到“/usr/local/src/”目录下;3、进入“src/”目录;4、解压源码包;5、创建mongodb文件目录;6、将文件复制到“mongodb/”目录;7、创建mongodb配置文件并修改配置即可。

How to install mongo extension in php7.0

本教程操作环境:Windows7系统、php7.0版、Dell G3电脑。

php7.0怎么安装mongo扩展?

PHP7源码安装MongoDB和MongoDB拓展

一、安装MongoDB

1.创建mongodb用户组和用户

groupadd mongodb
useradd -r -g mongodb -s /sbin/nologin -M mongodb
Copy after login

2.下载mongodb源码包,并将源码包放到/usr/local/src/目录下

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

这里用的是 mongodb-linux-x86_64-rhel62-3.2.10.tgz

下载地址:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.10.tgz

3.进入src/目录

cd /usr/local/src/
Copy after login
Copy after login

4.解压源码包

tar -zxf mongodb-linux-x86_64-rhel62-3.2.10.tgz
Copy after login

5.创建mongodb文件目录

mkdir -p /usr/local/mongodb/data
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/run/mongodb
mkdir -p /var/log/mongodb
Copy after login

6.将文件复制到mongodb/目录

cp -R /usr/local/src/mongodb-linux-x86_64-rhel62-3.2.10/. /usr/local/mongodb
Copy after login

7.创建mongodb配置文件mongodb.conf

vim /usr/local/mongodb/conf/mongodb.conf
Copy after login

8.添加下面内容,保存退出

dbpath=/usr/local/mongodb/data #数据目录存在位置
logpath=/var/log/mongodb/mongodb.log #日志文件存放目录
logappend=true #写日志的模式:设置为true为追加
fork=true  #以守护程序的方式启用,即在后台运行
verbose=true
vvvv=true #启动verbose冗长信息,它的级别有 vv~vvvvv,v越多级别越高,在日志文件中记录的信息越详细
maxConns=20000 #默认值:取决于系统(即的ulimit和文件描述符)限制。MongoDB中不会限制其自身的连接
pidfilepath=/var/run/mongodb/mongodb.pid
directoryperdb=true #数据目录存储模式,如果直接修改原来的数据会不见了
profile=0 #数据库分析等级设置,0 关 2 开。包括所有操作。 1 开。仅包括慢操作
slowms=200 #记录profile分析的慢查询的时间,默认是100毫秒
quiet=true
syncdelay=60 #刷写数据到日志的频率,通过fsync操作数据。默认60秒
#port=27017  #端口
#bind_ip = 10.1.146.163 #IP
#auth=true  #开始认证
#nohttpinterface=false #28017 端口开启的服务。默认false,支持
#notablescan=false#不禁止表扫描操作
#cpu=true #设置为true会强制mongodb每4s报告cpu利用率和io等待,把日志信息写到标准输出或日志文件
Copy after login

9.修改mongodb目录权限

chown -R mongodb:mongodb /usr/local/mongodb
chown -R mongodb:mongodb /var/run/mongodb
chown -R mongodb:mongodb /var/log/mongodb
Copy after login

10.将mongodb命令加入环境变量,修改profile文件

vim /etc/profile
Copy after login

11.修改为下面内容,保存退出

PATH=/usr/local/mysql/bin:/usr/local/php/bin:/usr/local/redis/bin:/usr/local/mongodb/bin:$PATH
Copy after login

12.使/etc/profile里的配置立即生效

source /etc/profile
Copy after login

13.将mongodb服务脚本加入到init.d/目录,创建mongod文件

vim /etc/init.d/mongod
Copy after login

14.加入下面内容,保存退出

#!/bin/sh  
# chkconfig: 2345 93 18
# description:MongoDB  
#默认参数设置
#mongodb 家目录
MONGODB_HOME=/usr/local/mongodb
#mongodb 启动命令
MONGODB_BIN=$MONGODB_HOME/bin/mongod
#mongodb 配置文件
MONGODB_CONF=$MONGODB_HOME/conf/mongodb.conf
MONGODB_PID=/var/run/mongodb/mongodb.pid
#最大文件打开数量限制
SYSTEM_MAXFD=65535
#mongodb 名字  
MONGODB_NAME="mongodb"
. /etc/rc.d/init.d/functions
if [ ! -f $MONGODB_BIN ]
then
    echo "$MONGODB_NAME startup: $MONGODB_BIN not exists! "  
    exit
fi
start(){
    ulimit -HSn $SYSTEM_MAXFD
    $MONGODB_BIN --config="$MONGODB_CONF"  
    ret=$?
    if [ $ret -eq 0 ]; then
        action $"Starting $MONGODB_NAME: " /bin/true
    else
        action $"Starting $MONGODB_NAME: " /bin/false
    fi
}
stop(){
    PID=$(ps aux |grep "$MONGODB_NAME" |grep "$MONGODB_CONF" |grep -v grep |wc -l) 
    if [[ $PID -eq 0  ]];then
        action $"Stopping $MONGODB_NAME: " /bin/false
        exit
    fi
    kill -HUP `cat $MONGODB_PID`
    ret=$?
    if [ $ret -eq 0 ]; then
        action $"Stopping $MONGODB_NAME: " /bin/true
        rm -f $MONGODB_PID
    else   
        action $"Stopping $MONGODB_NAME: " /bin/false
    fi
}
restart(){
    stop
    sleep 2
    start
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
    status $prog
        ;;
    restart)
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}"
esac
Copy after login

15.为mongod添加可执行权限

chmod +x /etc/init.d/mongod
Copy after login

16.将mongodb加入系统服务

chkconfig --add mongod
Copy after login

17.修改服务的默认启动等级

chkconfig mongod on
Copy after login

18.启动mongodb

service mongod start
Copy after login

二、PHP7安装MongoDB拓展

1.下载php7 mongodb拓展包,并将源码包放到/usr/local/src/目录下

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

这里用的是 mongodb-1.1.9.tgz

下载地址:http://pecl.php.net/get/mongodb-1.1.9.tgz

2.进入src/目录

cd /usr/local/src/
Copy after login
Copy after login

3.解压拓展包

tar -zxf mongodb-1.1.9.tgz
Copy after login

4.进入mongodb拓展目录,编译安装拓展

cd mongodb-1.1.9/
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
Copy after login

5.修改php.ini文件

vim /usr/local/php/etc/php.ini
Copy after login

6.添加mongodb.so扩展配置,保存退出

extension=mongodb.so
Copy after login

7.重启Apache或php-fpm

service httpd restart
service php-fpm restart
Copy after login

8.在web目录下添加php文件,如/usr/local/apache/htdocs/mongodb.php 或 /usr/local/nginx/html/mongodb.php

<?php
$manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert([&#39;x&#39; => 1, &#39;class&#39;=>&#39;toefl&#39;, &#39;num&#39; => &#39;18&#39;]);
$bulk->insert([&#39;x&#39; => 2, &#39;class&#39;=>&#39;ielts&#39;, &#39;num&#39; => &#39;26&#39;]);
$bulk->insert([&#39;x&#39; => 3, &#39;class&#39;=>&#39;sat&#39;, &#39;num&#39; => &#39;35&#39;]);
$manager->executeBulkWrite(&#39;test.log&#39;, $bulk);
$filter = [&#39;x&#39; => [&#39;$gt&#39; => 1]];
$options = [
    &#39;projection&#39; => [&#39;_id&#39; => 0],
    &#39;sort&#39; => [&#39;x&#39; => -1],
];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery(&#39;test.log&#39;, $query);
foreach ($cursor as $document) {
    print_r($document);
}
Copy after login

访问URL,如:http://192.168.8.9/mongodb.php

页面显示正常,则配置成功

MongoDB安装完毕!

推荐学习:《PHP视频教程

The above is the detailed content of How to install mongo extension in php7.0. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template