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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks 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)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

How to choose a database for GitLab on CentOS How to choose a database for GitLab on CentOS Apr 14, 2025 pm 04:48 PM

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

How to set up users in mongodb How to set up users in mongodb Apr 12, 2025 am 08:51 AM

To set up a MongoDB user, follow these steps: 1. Connect to the server and create an administrator user. 2. Create a database to grant users access. 3. Use the createUser command to create a user and specify their role and database access rights. 4. Use the getUsers command to check the created user. 5. Optionally set other permissions or grant users permissions to a specific collection.

How to encrypt data in Debian MongoDB How to encrypt data in Debian MongoDB Apr 12, 2025 pm 08:03 PM

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

What are the tools to connect to mongodb What are the tools to connect to mongodb Apr 12, 2025 am 06:51 AM

The main tools for connecting to MongoDB are: 1. MongoDB Shell, suitable for quickly viewing data and performing simple operations; 2. Programming language drivers (such as PyMongo, MongoDB Java Driver, MongoDB Node.js Driver), suitable for application development, but you need to master the usage methods; 3. GUI tools (such as Robo 3T, Compass) provide a graphical interface for beginners and quick data viewing. When selecting tools, you need to consider application scenarios and technology stacks, and pay attention to connection string configuration, permission management and performance optimization, such as using connection pools and indexes.

How to start mongodb How to start mongodb Apr 12, 2025 am 08:39 AM

To start the MongoDB server: On a Unix system, run the mongod command. On Windows, run the mongod.exe command. Optional: Set the configuration using the --dbpath, --port, --auth, or --replSet options. Use the mongo command to verify that the connection is successful.

See all articles