Mongodb安裝php
以centos6.x_x_64位系統爲例,咱們使用yum安裝html
1.添加yum源nginx
在/etc/yum.repos.d/目錄下建立文件10genmon.repo(名字隨意,.repo結尾就行),它包含MongoDB倉庫的配置信息git
[root@zwj yum.repos.d]# pwd /etc/yum.repos.d [root@zwj yum.repos.d]# cat 10genmon.repo [10gen] name=10gen Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64 gpgcheck=0
2.使用yum安裝github
[root@zwj yum.repos.d]# yum install mongodb-org #我已經安裝過了,全部不會重複安裝 Loaded plugins: fastestmirror, security Setting up Install Process Loading mirror speeds from cached hostfile Package mongodb-org-2.6.12-1.x86_64 already installed and latest version Nothing to do [root@zwj yum.repos.d]# rpm -qa|grep mongod
mongodb-org-shell-2.6.12-1.x86_64
mongodb-org-server-2.6.12-1.x86_64
mongodb-org-tools-2.6.12-1.x86_64
mongodb-org-2.6.12-1.x86_64
mongodb-org-mongos-2.6.12-1.x86_64
[root@zwj yum.repos.d]#
3.根據須要修改/etc/mongo.conf文件web
logpath=/var/log/mongodb/mongod.log #mongo日誌文件mongodb
dbpath=/data/mongo #數據庫data存放路徑shell
pidfilepath=/var/run/mongodb/mongod.pid #進程pid數據庫
mkdir -p /var/log/mongodb
mkdir -p /data/mongo
mkdir -p /var/run/mongodbcentos
chown mongod.mongod /var/log/mongodb -R
chown mongod.mongod /var/run/mongodb -R
chown mongod.mongod /data/mongo -R
到這裏就能夠經過/etc/init.d/mongod start啓動mongd服務了,默認端口27017,能夠在/etc/mongod.conf修改
4.考慮到安全問題咱們要給mongd數據庫添加用戶信息
想要啓用mongod認證須要在/etc/mongod.conf中設置auth=true.可是啓用安全以前必定要先添加一超級用戶以下演示:
MongoDB shell version: 2.6.12 connecting to: test > use admin switched to db admin > db.addUser('root','xxxx') 1 > exit bye
一旦設置認真,全部的操縱均須要通過認證
[root@zwj mongodb]# mongo MongoDB shell version: 2.6.12 connecting to: test
> use admin
switched to db admin
> show collections
2017-05-10T18:27:26.624+0800 error: {
"$err" : "not authorized for query on admin.system.namespaces", #提示已經沒有權限了
"code" : 13
} at src/mongo/shell/query.js:131
> db.auth('root','xxxx')
1 #1表示已經認真成功,這時候能夠做任何事情了,由於是管理員帳號
> show collections
system.indexes
system.users
system.version
>
咱們不可能把管理員帳號非每個使用者,因此咱們能夠給某個數據庫創建帳號,而後提供給指定用戶
[root@zwj yum.repos.d]# mongo TestDB MongoDB shell version: 2.6.12 connecting to: TestDB > use admin switched to db admin > db.auth('root','xxxx') 1 > use TestDB switched to db TestDB > db.addUser('test','test') #TestDB的專屬帳號
關於mongodb帳號問題能夠參考 http://www.cnblogs.com/valor-xh/p/6369432.html
Mongodb可視化管理工具rockmongo
rockMongo是PHP5寫的一個MongoDB管理工具。另外咱們要保證mongodb啓動時已經設置了auth=true選項.不然就太不安全了。
經過 Rockmongo 你能夠管理 MongoDB服務,數據庫,集合,文檔,索引等等。它提供了很是人性化的操做。相似 phpMyAdmin(PHP開發的MySql管理工具)。
Rockmongo 下載地址:https://github.com/iwind/rockmongo.git
2.安裝php和php-fpm
#檢查是否安裝成功
[root@zwj mongodb]# php -v PHP 5.3.3 (cli) (built: Mar 22 2017 12:27:09) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies [root@zwj mongodb]# /etc/init.d/php-fpm status php-fpm (pid 13766) is running...
3.php-mongo驅動
extension=mongo.so">>/etc/php.ini
4.獲取rockmongo源碼程序並放到nginx(或其餘web服務器)的資源路徑,並根據自身狀況修改config.php文件
[root@zwj ~]# cat /usr/share/nginx/html/rockmongo/config.php <?php /** * RockMongo configuration * * Defining default options and server configuration * @package rockmongo */ $MONGO = array(); $MONGO["features"]["log_query"] = "on";//log queries $MONGO["features"]["theme"] = "default";//theme $MONGO["features"]["plugins"] = "on";//plugins $i = 0; /** * Configuration of MongoDB servers * * @see more details at http://rockmongo.com/wiki/configuration?lang=en_us */ $MONGO["servers"][$i]["mongo_name"] = "本身命名一個名字";//mongo server name //$MONGO["servers"][$i]["mongo_sock"] = "/var/run/mongo.sock";//mongo socket path (instead of host and port) $MONGO["servers"][$i]["mongo_host"] = "127.0.0.1";//mongo host $MONGO["servers"][$i]["mongo_port"] = "27017";//mongo port,須要和/etc/mongod.conf中一致,不然rockmongo程序找不到咱們的mongod服務 $MONGO["servers"][$i]["mongo_timeout"] = 0;//mongo connection timeout //follwo 3 lines only function when $MONGO["servers"][$i]["mongo_auth"] = false
//test,test這裏我提早給TestDB數據庫建立的用戶,密碼,根據本身須要決定是否設置此項.,這裏的設置跟下面test_admin想關聯. $MONGO["servers"][$i]["mongo_db"] = "TestDB";//default mongo db to connect, works only if mongo_auth=false $MONGO["servers"][$i]["mongo_user"] = "test";//mongo authentication user name, works only if mongo_auth=fals $MONGO["servers"][$i]["mongo_pass"] = "test";//mongo authentication password, works only if mongo_auth=false $MONGO["servers"][$i]["mongo_auth"] =true;//enable mongo authentication? $MONGO["servers"][$i]["control_auth"] = true;//enable control users, works only if mongo_auth=false
//test_admin是當「$MONGO["servers"][$i]["mongo_auth"] =false;//enable mongo authentication?」時使用的admin登錄名,並非真正的admin用戶,從上面能夠了解到,實際上是一個只能操做
//TestDB的"假的admin",而且它內部最終使用的是'test'帳戶以及密碼'test' $MONGO["servers"][$i]["control_users"]["test_admin"] = "test_admin";//one of control users ["USERNAME"]=PASSWORD, works only if mongo_auth=false $MONGO["servers"][$i]["ui_only_dbs"] = "";//databases to display $MONGO["servers"][$i]["ui_hide_dbs"] = "";//databases to hide $MONGO["servers"][$i]["ui_hide_collections"] = "";//collections to hide $MONGO["servers"][$i]["ui_hide_system_collections"] = false;//whether hide the system collections //$MONGO["servers"][$i]["docs_nature_order"] = false;//whether show documents by nature order, default is by _id field //$MONGO["servers"][$i]["docs_render"] = "default";//document highlight render, can be "default" or "plain" $i ++; /** * mini configuration for another mongo server */ /** $MONGO["servers"][$i]["mongo_name"] = "Localhost2"; $MONGO["servers"][$i]["mongo_host"] = "127.0.0.1"; $MONGO["servers"][$i]["mongo_port"] = "27017"; $MONGO["servers"][$i]["control_users"]["admin"] = "password"; $i ++; **/ ?>
4.配置nginx,把對rockmongo應用的相關請求所有交給fastcgi_pass處理
[root@zwj ~]# vi /etc/nginx/conf.d/rockmongo.conf server { listen 27016; #rockmongo對外的監聽端口 root /usr/share/nginx/html/rockmongo; try_files $uri /index.php; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
5.再保證php,nginx,mongodb工做正常的狀況下,測試一下,瀏覽器輸入host:27016便可
6.登陸
兩種登陸方式:
1.認證模式,在config.php中設置以下:
$MONGO["servers"][$i]["mongo_auth"] =true;//enable mongo authentication?
下圖是使用admin登陸的,因此除了admin數據庫外還能夠看到TestDB
下圖是用test用戶登陸的,只可以管理TestDB
2.無認證方式,要在config.php中設置以下:
$MONGO["servers"][$i]["mongo_auth"] =false;//enable mongo authentication?
$MONGO["servers"][$i]["mongo_db"] = "TestDB";//default mongo db to connect, works only if mongo_auth=false #默認訪問的DB
$MONGO["servers"][$i]["mongo_user"] = "test";//mongo authentication user name, works only if mongo_auth=fals #TestDB對應的真實用戶名
$MONGO["servers"][$i]["mongo_pass"] = "test";//mongo authentication password, works only if mongo_auth=false #TestDB對應的真實密碼
$MONGO["servers"][$i]["control_auth"] = true;//enable control users, works only if mongo_auth=false
$MONGO["servers"][$i]["control_users"]["test_admin"] = "test_admin"
而後使用test_admin帳戶做爲假的admin帳戶登陸,這裏的test_admin表示能夠查看管理TestDB的用戶,或許這麼作緣由多是不想把TestDB的真實用戶信息暴露出來吧-_-
測試過程當中認證與不認證方式切換時會有些問題,須要重啓mongodb服務(固然很煩,,),建議使用第一種就好
最後給一個mongo3.4的yum源,官網更詳盡,https://www.mongodb.com/download-center
[root@zwj mongodb_yum]# cat mongodb-org-3.4.repo [mongodb-org-3.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc