PHP installation Configuration MongoDB database connection extension step in Linux system

Source: Internet
Author: User
Tags foreach auth fpm install php mongodb mongodb server php mongodb 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
To achieve the purpose:
Install PHP MongoDB database extensions, connect MongoDB database via PHP program
Specific actions:
First, the installation of PHP MongoDB database extensions (on the Web server 192.168.21.127 operation)
Download Address: 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
/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
Vi/usr/local/php/etc/php.ini #编辑, add the following code on 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 illustration:
Second, create test database (operate on MongoDB database server)
Executing on the MongoDB
cd/usr/local/mongodb/bin/
./mongo #进入数据库
#创建数据库: OSYUNWEIDB, create a new table in the database, insert data
Use Osyunweidb #创建数据库osyunweidb If you leave without doing anything, the library will be deleted by the system.
Db.createcollection ("osyunweidb_table") #创建表osyunweidb_table
Show Collections #查看
Db.osyunweidb_table.insert ({uid:1,name: "System Operation Dimension", URL: "http://www.osyunwi.com", Content: "System Operation Dimension 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 default does not have a username and password, as long as the connection on the service, you can log in, do all the operation.
2, can be set up through the configuration file, to open the login verification function.
3, only switch to the admin database, add the account number is the administrator account.
4, the Administrator account can be 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 and 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 number 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 ') #删除用户
###############################################################
Set up MongoDB configuration file, turn on login authentication function (operate 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 the page http://192.168.21.127/test.php The following illustration shows that the database connection was successful.
At this point, Linux under the PHP installation configuration MongoDB database connection extension completed.
Extended reading:
MongoDB Database Client Tools
Mongovue, as shown in the following illustration:
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.