Description
Operating system: CentOS 5.X 64-bit
IP Address: 192.168.21.130
IP Address: 192.168.21.127
PHP Installation path:/usr/local/php
Install PHP MongoDB database extensions, connect MongoDB database via PHP program
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 #解压
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
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
Service PHP-FPM Reload #重新加载php-fpm
Add a PHP test page under the site root directory
vi/usr/local/nginx/html/phpinfo.php #编辑
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)
cd/usr/local/mongodb/bin/
#创建数据库: 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
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
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/
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 ') #添加用户认证
cd/usr/local/mongodb/bin/
Use admin #切换到admin数据库, add the account number is the administrator account.
Db.adduser (' root ', ' 123456 ') #添加管理员账号: root password: 123456
Db.auth (' root ', ' 123456 ') #添加管理员用户认证 to manage all databases after authentication
###############################################################
#删除数据库, delete table operations
cd/usr/local/mongodb/bin/
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 #编辑配置文件
cd/usr/local/mongodb/bin/
./mongo-uosyunweidbuser-p123456 127.0.0.1:27017/osyunweidb #进入mongodb控制台
cd/usr/local/mongodb/bin/
./mongo-uroot-p123456 127.0.0.1:27017/admin
V. PHP connection Test Database (operation on Web server 192.168.21.127)
vi/usr/local/nginx/html/test.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) {#输出数据
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> ";
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.
MongoDB Database Client Tools
Mongovue, as shown in the following illustration: