Linux under PHP installation configuration MongoDB database connection extension

Source: Internet
Author: User
Tags fpm mongodb server phpinfo

Description

Operating system: CentOS 5.X 64-bit

MongoDB Database server:

IP Address: 192.168.21.130

Web server:

IP Address: 192.168.21.127

PHP Installation path:/usr/local/php

Purpose of implementation:

Install the MongoDB database extension of PHP, connect the MONGODB database via PHP program

Specific operation:

I. Installing the MONGODB database extension for PHP (operating on Web server 192.168.21.127)

: http://pecl.php.net/get/mongo-1.5.1.tgz

Upload mongo-1.5.1.tgz to/usr/local/src directory

Tar zxvf mongo-1.5.1.tgz #解压

CD mongo-1.5.1

/usr/local/php/bin/phpize

./configure--with-php-config=/usr/local/php/bin/php-config

Make #编译

Make install #安装, the following interface appears after completion

System operation and maintenance www.osyunwei.com warm reminder: qihang01 original Content © Copyright, reproduced please indicate the source and the original chain

/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

Vi/usr/local/php/etc/php.ini #编辑, add the following code to the last line

extension= "Mongo.so"

: wq! #保存退出

Service PHP-FPM Reload #重新加载php-fpm

Add a PHP test page under the site root directory

vi/usr/local/nginx/html/phpinfo.php #编辑

<?php

Phpinfo ();

?>

: wq! #保存退出

Open the page above, http://192.168.21.127/phpinfo.php

As shown in the following:

Ii. Creating a test database (operating on the MongoDB database server)

Execute on MongoDB

cd/usr/local/mongodb/bin/

./mongo #进入数据库

#创建数据库: OSYUNWEIDB, create a new table in the database, insert the data

Use Osyunweidb #创建数据库osyunweidb If nothing is left, the library will be deleted by the system.

Db.createcollection ("osyunweidb_table") #创建表osyunweidb_table

Show Collections #查看

Db.osyunweidb_table.insert ({uid:1,name: "System Ops", URL: "http://www.osyunwi.com", Content: "System operations official Website"}) # Inserting data into table osyunweidb_table

Db.osyunweidb.find ();

Show DBS

Third, create the database account (in the MongoDB database server operation)

===============================================================

1, MongoDB database by default no user name and password, as long as the connection on the service, you can log in, do all the operation.

2. The login verification function can be turned on by configuration file settings.

3, only switch to the admin database, add the account is the Administrator account.

4, the Administrator account can be the MONGODB server all the database management (but: the default can only manage their own admin database, to manage other databases, you need to first in the Admin database authentication)

===============================================================

#添加普通账号

cd/usr/local/mongodb/bin/

./mongo #进入数据库

Use OSYUNWEIDB

Db.adduser (' Osyunweidbuser ', ' 123456 ') #账号: osyunweidbuser Password: 123456 permissions: Read/write

#db. AddUser (' Osyunweidbuser ', ' 123456 ', True) #账号: osyunweidbuser Password: 123456 permissions: Read-only

Db.auth (' Osyunweidbuser ', ' 123456 ') #添加用户认证

Exit #退出

#添加管理员账号

cd/usr/local/mongodb/bin/

./mongo #进入数据库

Show DBS #列出所有数据库

Use admin #切换到admin数据库, add the account is the Administrator account.

Show collections

Db.system.users.find ()

Db.adduser (' root ', ' 123456 ') #添加管理员账号: root password: 123456

Db.auth (' root ', ' 123456 ') #添加管理员用户认证 to manage all databases after authentication

Exit #退出

###############################################################

#删除数据库, delete table operations

cd/usr/local/mongodb/bin/

./mongo

Use Osyunweidb #进入数据库

Db.dropdatabase () #删除数据库

Db.osyunweidb_table.drop () #删除表

Db.removeuser (' Osyunweidbuser ') #删除用户

System operation and maintenance www.osyunwei.com warm reminder: qihang01 original Content © Copyright, reproduced please indicate the source and the original chain

###############################################################

Iv. setting up MongoDB configuration file, turn on login verification function (operation on MONGODB database server)

Vi/usr/local/mongodb/mongodb.conf #编辑配置文件

Auth=true #开启认证

: wq! #保存配置

Service Mongod Stop #停止

Service Mongod Start #启动

cd/usr/local/mongodb/bin/

./mongo-uosyunweidbuser-p123456 127.0.0.1:27017/osyunweidb #进入mongodb控制台

Exit #退出

cd/usr/local/mongodb/bin/

./mongo-uroot-p123456 127.0.0.1:27017/admin

Db.system.users.find ()

Use OSYUNWEIDB

Show collections

Exit #退出

V. PHP connection Test Database (operation on Web server 192.168.21.127)

vi/usr/local/nginx/html/test.php

<?php

$mongo = new MONGO ("192.168.21.130:27017"); #连接数据库服务器

$db = $mongo->selectdb (' osyunweidb '); #连接数据库

$db->authenticate ("Osyunweidbuser", "123456"); #数据库连接账号和密码

$collection = $db->selectcollection ("osyunweidb_table"); #选择数据库中的表

$result = $collection->find (); #读取数据

foreach ($result as $value) {#输出数据

Print_r ($value);

}

echo "<br>";

foreach ($result as $value) {

echo "Serial number:". $value ["UID"]. " <br> ";

echo "Site name:". $value ["name"]. " <br> ";

echo "URL:". $value ["url"]. " <br> ";

echo "Remarks:". $value ["Content"]. " <br> ";

}

$mongo->close ();

?>

Open page http://192.168.21.127/test.php as shown, the database connection is successful.

At this point, Linux under the PHP installation configuration MongoDB database connection extension completed.

Extended reading:

MongoDB Database Client Tool

Mongovue, as shown in:

Linux under PHP installation configuration MongoDB database connection extension

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.