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

How to install mongo extension in php7.0

Nov 21, 2022 am 10:25 AM
php7 mongodb

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

What is the use of net4.0 What is the use of net4.0 May 10, 2024 am 01:09 AM

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

How to make PHP5.6 and PHP7 coexist through Nginx configuration on the same server? How to make PHP5.6 and PHP7 coexist through Nginx configuration on the same server? Apr 01, 2025 pm 03:15 PM

Running multiple PHP versions simultaneously in the same system is a common requirement, especially when different projects depend on different versions of PHP. How to be on the same...

Compilation and installation of Redis on Apple M1 chip Mac failed. How to troubleshoot PHP7.3 compilation errors? Compilation and installation of Redis on Apple M1 chip Mac failed. How to troubleshoot PHP7.3 compilation errors? Mar 31, 2025 pm 11:39 PM

Problems and solutions encountered when compiling and installing Redis on Apple M1 chip Mac, many users may...

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

Comparison of Redis queues and MySQL stability: Why is Redis prone to data loss? Comparison of Redis queues and MySQL stability: Why is Redis prone to data loss? Apr 01, 2025 pm 02:24 PM

Comparison of Redis queues and MySQL stability: Why is Redis prone to data loss? In the development environment, using PHP7.2 and ThinkPHP frameworks, we often face the choice of cooperation...

See all articles