How to install mongo extension in php7.0
php7.0安装mongo扩展的方法:1、创建mongodb用户组和用户;2、下载mongodb源码包,并将源码包放到“/usr/local/src/”目录下;3、进入“src/”目录;4、解压源码包;5、创建mongodb文件目录;6、将文件复制到“mongodb/”目录;7、创建mongodb配置文件并修改配置即可。
本教程操作环境: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
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/
4.解压源码包
tar -zxf mongodb-linux-x86_64-rhel62-3.2.10.tgz
5.创建mongodb文件目录
mkdir -p /usr/local/mongodb/data mkdir -p /usr/local/mongodb/conf mkdir -p /var/run/mongodb mkdir -p /var/log/mongodb
6.将文件复制到mongodb/目录
cp -R /usr/local/src/mongodb-linux-x86_64-rhel62-3.2.10/. /usr/local/mongodb
7.创建mongodb配置文件mongodb.conf
vim /usr/local/mongodb/conf/mongodb.conf
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等待,把日志信息写到标准输出或日志文件
9.修改mongodb目录权限
chown -R mongodb:mongodb /usr/local/mongodb chown -R mongodb:mongodb /var/run/mongodb chown -R mongodb:mongodb /var/log/mongodb
10.将mongodb命令加入环境变量,修改profile文件
vim /etc/profile
11.修改为下面内容,保存退出
PATH=/usr/local/mysql/bin:/usr/local/php/bin:/usr/local/redis/bin:/usr/local/mongodb/bin:$PATH
12.使/etc/profile里的配置立即生效
source /etc/profile
13.将mongodb服务脚本加入到init.d/目录,创建mongod文件
vim /etc/init.d/mongod
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
15.为mongod添加可执行权限
chmod +x /etc/init.d/mongod
16.将mongodb加入系统服务
chkconfig --add mongod
17.修改服务的默认启动等级
chkconfig mongod on
18.启动mongodb
service mongod start
二、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/
3.解压拓展包
tar -zxf mongodb-1.1.9.tgz
4.进入mongodb拓展目录,编译安装拓展
cd mongodb-1.1.9/ phpize ./configure --with-php-config=/usr/local/php/bin/php-config make && make install
5.修改php.ini文件
vim /usr/local/php/etc/php.ini
6.添加mongodb.so扩展配置,保存退出
extension=mongodb.so
7.重启Apache或php-fpm
service httpd restart service php-fpm restart
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(['x' => 1, 'class'=>'toefl', 'num' => '18']); $bulk->insert(['x' => 2, 'class'=>'ielts', 'num' => '26']); $bulk->insert(['x' => 3, 'class'=>'sat', 'num' => '35']); $manager->executeBulkWrite('test.log', $bulk); $filter = ['x' => ['$gt' => 1]]; $options = [ 'projection' => ['_id' => 0], 'sort' => ['x' => -1], ]; $query = new MongoDB\Driver\Query($filter, $options); $cursor = $manager->executeQuery('test.log', $query); foreach ($cursor as $document) { print_r($document); }
访问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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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...

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

.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.

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...

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

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

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? In the development environment, using PHP7.2 and ThinkPHP frameworks, we often face the choice of cooperation...
