NoSQL(四)

mongodb介紹

https://www.yiibai.com/mongodb/mongodb_drop_collection.htmlphp

1.文檔性數據庫相似於json對象,分佈式html

mongodb安裝

1.基本思路就是建立一個官方的yum源,咱們這裏安裝最新的3.6版本node

[root@centos-02 ~]# cd /etc/yum.repos.d/
[root@centos-02 yum.repos.d]# vim mongodb.repo
[root@centos-02 yum.repos.d]# 
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

2.而後查看下源中有沒有mongodb這個包,ok有mysql

[root@centos-02 yum.repos.d]# 
[root@centos-02 yum.repos.d]# yum list|grep mongodb
collectd-write_mongodb.x86_64             5.8.0-1.el7                  epel     
mongodb.x86_64                            2.6.12-6.el7                 epel     
mongodb-org.x86_64 3.6.3-1.el7 mongodb-org-3.6 mongodb-org-mongos.x86_64 3.6.3-1.el7 mongodb-org-3.6 mongodb-org-server.x86_64 3.6.3-1.el7 mongodb-org-3.6 mongodb-org-shell.x86_64 3.6.3-1.el7 mongodb-org-3.6 mongodb-org-tools.x86_64 3.6.3-1.el7 mongodb-org-3.6
mongodb-server.x86_64                     2.6.12-6.el7                 epel     
mongodb-test.x86_64                       2.6.12-6.el7                 epel     
nodejs-mongodb.noarch                     1.4.7-1.el7                  epel     
php-mongodb.noarch                        1.0.4-1.el7                  epel     
php-pecl-mongodb.x86_64                   1.1.10-1.el7                 epel     
poco-mongodb.x86_64                       1.6.1-3.el7                  epel     
syslog-ng-mongodb.x86_64                  3.5.6-3.el7                  epel     
[root@centos-02 yum.repos.d]#

3.安裝mongodblinux

[root@centos-02 yum.repos.d]# yum install -y mongodb-org

鏈接mongodb

1.若是想綁定多個ip訪問,添加多個ip用逗號分割nginx

bindIp: 127.0.0.1,192.168.133.88

2.啓動mongodbgit

[root@centos-02 yum.repos.d]# systemctl start mongod
[root@centos-02 yum.repos.d]# ps aux|grep mongod
mongod    48151  5.1  4.2 1005608 42792 ?       Sl   17:28   0:03 /usr/bin/mongod -f /etc/mongod.conf
root      48176  0.0  0.0 112680   948 pts/0    R+   17:29   0:00 grep --color=auto mongod
[root@centos-02 yum.repos.d]# netstat -lntp |grep mongod
tcp        0      0 192.168.133.88:27017    0.0.0.0:*               LISTEN      48151/mongod        
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      48151/mongod        
[root@centos-02 yum.repos.d]#

3.在本機直接運行mongo進入到mongodb shell中github

[root@centos-02 yum.repos.d]# mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2018-04-01T17:28:16.465+0800 I CONTROL  [initandlisten] 
2018-04-01T17:28:16.467+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-01T17:28:16.467+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-01T17:28:16.468+0800 I CONTROL  [initandlisten] 
2018-04-01T17:28:16.469+0800 I CONTROL  [initandlisten] 
2018-04-01T17:28:16.470+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-01T17:28:16.470+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-01T17:28:16.470+0800 I CONTROL  [initandlisten] 
>

4.鏈接到指定的ip和端口web

[root@centos-02 yum.repos.d]# mongo --host 192.168.133.88 --port 27017
MongoDB shell version v3.6.3
connecting to: mongodb://192.168.133.88:27017/
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-04-01T17:28:16.465+0800 I CONTROL  [initandlisten] 
2018-04-01T17:28:16.467+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-01T17:28:16.467+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-01T17:28:16.468+0800 I CONTROL  [initandlisten] 
2018-04-01T17:28:16.469+0800 I CONTROL  [initandlisten] 
2018-04-01T17:28:16.470+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-01T17:28:16.470+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-01T17:28:16.470+0800 I CONTROL  [initandlisten] 
>

5.若是設置了驗證,須要輸入用戶和密碼和鏈接的加密庫redis

[root@centos-02 yum.repos.d]# mongo -uusername -ppassword --authenticationDatabase db

mongodb用戶管理

1.如何給用戶設置密碼,切換到admin庫,執行建立用戶語句(語句包括,建立的用戶名,用戶的描述(能夠不寫),用戶密碼,什麼角色,角色在哪個庫)

[root@centos-02 yum.repos.d]# mongo --host 192.168.133.88 --port 27017
MongoDB shell version v3.6.3
connecting to: mongodb://192.168.133.88:27017/
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-04-01T17:28:16.465+0800 I CONTROL  [initandlisten] 
2018-04-01T17:28:16.467+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-01T17:28:16.467+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-01T17:28:16.468+0800 I CONTROL  [initandlisten] 
2018-04-01T17:28:16.469+0800 I CONTROL  [initandlisten] 
2018-04-01T17:28:16.470+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-01T17:28:16.470+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-01T17:28:16.470+0800 I CONTROL  [initandlisten] 
> use admin
switched to db admin
> db.createUser( { user: "admin", customData: {description: "superuser"}, pwd: "admin122", roles: [ { role: "root", db: "admin" } ] } )
Successfully added user: {
	"user" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
>

2.列出全部用戶,須要事先切換的admin庫

> db.system.users.find()
{ "_id" : "admin.test1", "user" : "test1", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "qkIyRd0o38eqKOlEx3vwHA==", "storedKey" : 
"JVrdMZud+jjZb9awIWxSrLwixxY=", "serverKey" : "vmaJHnyV8tfPyvxqpuF4ipe/MJc=" } }, "roles" : [ { "role" : "readWrite", "db" : "db1" }, { "role" : "read", "db" : "db2" } ] } { "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "OMpr/hze3FqzgKmv45sUBw==", "storedKey" :
"EkLLK+LNAGMVV3ddgxYoZCXGlPU=", "serverKey" : "h1UxGUDeFB9C1N7dRyN30bDhFOA=" } }, "customData" : { "description" : "superuser" }, "roles" : [ { "role" : "root", "db" : "admin" } ] } >

3.查看當前庫下全部的用戶

> show users
{
	"_id" : "admin.admin",
	"user" : "admin",
	"db" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
{
	"_id" : "admin.test1",
	"user" : "test1",
	"db" : "admin",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "db1"
		},
		{
			"role" : "read",
			"db" : "db2"
		}
	]
}
>

4.刪除一個用戶,怎麼刪除,首先建立一個測試用戶

> db.createUser({user:"linux",pwd:"linux",roles:[{role:"read",db:"testdb"}]})
Successfully added user: {
	"user" : "linux",
	"roles" : [
		{
			"role" : "read",
			"db" : "testdb"
		}
	]
}
>

5.刪除後查看用戶,發現linux用戶沒有了

> db.dropUser('linux')
true
> show users
{
	"_id" : "admin.admin",
	"user" : "admin",
	"db" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
{
	"_id" : "admin.test1",
	"user" : "test1",
	"db" : "admin",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "db1"
		},
		{
			"role" : "read",
			"db" : "db2"
		}
	]
}
>

6.use一個庫,若是這個庫不存在,則會建立這個庫。

> use testdb
switched to db testdb
>

7.若要用戶生效,還須要編輯配置啓動文件,在OPTIONS後面加上--auth,只有加了--auth,才能夠用用戶名密碼登陸,重啓mongo

[root@centos-02 yum.repos.d]# vim /usr/lib/systemd/system/mongod.service 
[root@centos-02 yum.repos.d]# 
Environment="OPTIONS=--auth -f /etc/mongod.conf"
[root@centos-02 yum.repos.d]# systemctl daemon-reload
[root@centos-02 yum.repos.d]# systemctl restart mongod
[root@centos-02 yum.repos.d]# ps aux|grep mongo
mongod    48251 60.1  4.2 1005608 42600 ?       Sl   18:26   0:25 /usr/bin/mongod --auth -f /etc/mongod.conf
root      48279  0.0  0.0 112680   944 pts/0    R+   18:27   0:00 grep --color=auto mong
[root@centos-02 yum.repos.d]#

8.測試用戶密碼登陸,咱們仍是用以前不用用戶和密碼的方式登陸查看admin庫發現不行了,用用戶名密碼登陸能夠

[root@centos-02 yum.repos.d]# mongo --host 192.168.133.88 --port 27017
MongoDB shell version v3.6.3
connecting to: mongodb://192.168.133.88:27017/
MongoDB server version: 3.6.3
> use admin
switched to db admin
> show users
2018-04-01T18:31:54.272+0800 E QUERY    [thread1] Error: not authorized on admin to execute command { usersInfo: 1.0, $db: "admin" } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1686:1
shellHelper.show@src/mongo/shell/utils.js:799:9
shellHelper@src/mongo/shell/utils.js:706:15
@(shellhelp2):1:1
>
[root@centos-02 yum.repos.d]# mongo --host 192.168.133.88 --port 27017 -u admin -p 'admin122' --authenticationDatabase "admin"
> use admin
switched to db admin
> show users
{
	"_id" : "admin.admin",
	"user" : "admin",
	"db" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
{
	"_id" : "admin.test1",
	"user" : "test1",
	"db" : "admin",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "db1"
		},
		{
			"role" : "read",
			"db" : "db2"
		}
	]
}
>

9.你們要記住,建立用戶的時候必需要針對一個庫,登陸的時候也須要針對這個庫作認證  

1.建立db1,針對db1受權一個用戶

> use db1
switched to db db1
> db.createUser( { user: "test1", pwd: "123aaa", roles: [ { role: "readWrite", db: "db1" }, {role: "read", db: "db2" } ] } )
Successfully added user: {
	"user" : "test1",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "db1"
		},
		{
			"role" : "read",
			"db" : "db2"
		}
	]
}
>

2.在db1裏show,在哪一個庫建立就在哪一個庫show

> show users
{
	"_id" : "db1.test1",
	"user" : "test1",
	"db" : "db1",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "db1"
		},
		{
			"role" : "read",
			"db" : "db2"
		}
	]
}
>

3.在命令行受權一個用戶對db1有權限

> use db1
switched to db db1
> db.auth('test1','123aaa')
1
>

mongodb建立集合、數據管理

1.咱們在db1中建立集合

> db.createCollection("mycol", { capped : true, size : 6142800, max : 10000 } )
{ "ok" : 1 }
>

1.查看集合

> show collections
mycol
> show tables
mycol
>

2.集合插入數據,沒有集合會自動建立集合

> db.Account.insert({AccountID:1,UserName:"123",password:"123456"}) 
WriteResult({ "nInserted" : 1 })
> show tables
Account
mycol
> db.Account.insert({AccountID:2,UserName:"222",password:"222222"})
WriteResult({ "nInserted" : 1 })
>

3.更新數據並查看結果

> db.Account.update({AccountID:1},{"$set":{"Age":20}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
查看全部文檔 > db.Account.find() { "_id" : ObjectId("5ac23df4fd2577e13af3e0ee"), "AccountID" : 1, "UserName" : "123", "password" : "123456", "Age" : 20 } { "_id" : ObjectId("5ac23e19fd2577e13af3e0ef"), "AccountID" : 2, "UserName" : "222", "password" : "222222" } >

4.根據條件查詢

> db.Account.find({AccountID:1})
{ "_id" : ObjectId("5ac23df4fd2577e13af3e0ee"), "AccountID" : 1, "UserName" : "123", "password" : "123456", "Age" : 20 }
>

5.根據條件刪除一行

>  db.Account.remove({AccountID:1})
WriteResult({ "nRemoved" : 1 })
> db.Account.find()
{ "_id" : ObjectId("5ac23e19fd2577e13af3e0ef"), "AccountID" : 2, "UserName" : "222", "password" : "222222" }
>

6.刪除全部的文檔,即刪除集合

> db.Account.drop()
true
> db.Account.find()
>

7.查看集合狀態

db.printCollectionStats()

PHP的mongodb擴展

1.mongodb對於php有兩個擴展,一個是mongo擴展,一個是mongodb擴展,mongo擴展是針對php5.X,mongodb是針對更高版本的

2.咱們開始安裝mongodb,下載mongodb

[root@centos-02 src]# cd /usr/local/src/
[root@centos-02 src]# wget http://pecl.php.net/get/mongodb-1.4.2.tgz

3.解壓

[root@centos-02 src]# tar zxvf mongodb-1.4.2.tgz

4.進入mongodb執行phpize

[root@centos-02 src]# cd mongodb-1.4.2
[root@centos-02 mongodb-1.4.2]# /usr/bin/phpize  (安裝php-fpm的執行/usr/local/php-frm/bin/phpize)

5.編譯

[root@centos-02 mongodb-1.4.2]# ./configure --with-php-config=/usr/bin/php-config (若是是nginx的php路徑爲/usr/local/php-fpm/bin/php-conf)

6.哈哈不支持低版本的php,從新下一個mongodb

configure: error: not supported. Need a PHP version >= 5.5.0 (found 5.4.16)
[root@centos-02 mongodb-1.4.2]#
[root@centos-02 src]# wget https://github.com/mongodb/mongo-php-driver-legacy/archive/master.zip

7.解壓

[root@centos-02 src]# unzip master.zip

8.進入mongodb執行phpize,編譯

[root@centos-02 src]# cd mongo-php-driver-legacy-master/
[root@centos-02 mongo-php-driver-legacy-master]# /usr/bin/phpize [root@centos-02 mongo-php-driver-legacy-master]# ./configure --with-php-config=/usr/bin/php-config

9.make && make install

[root@centos-02 mongo-php-driver-legacy-master]# make install
Installing shared extensions:     /usr/lib64/php/modules/
[root@centos-02 mongo-php-driver-legacy-master]# ls /usr/lib64/php/modules/
bcmath.so      json.so        mysqli.so      phar.so        xmlwriter.so
curl.so        ldap.so        mysql.so       redis.so       xsl.so
dom.so         mbstring.so    pdo_mysql.so   sqlite3.so     zip.so
fileinfo.so    memcache.so    pdo.so         wddx.so        
gd.so          mongo.so       pdo_sqlite.so  xmlreader.so   
[root@centos-02 mongo-php-driver-legacy-master]# ls /usr/lib64/php/modules/

10.編輯php.ini增長mongo.so

[root@centos-02 mongo-php-driver-legacy-master]# vim /etc/php.ini (nginx安裝的高版本php路徑爲/usr/local/php-fpm/etc/php.ini)
extension=mongo.so  (高版本的mongodb.so)

11.檢測是否加載了mongo模塊

[root@centos-02 mongo-php-driver-legacy-master]# /usr/bin/php -m|grep mongo
mongo
[root@centos-02 mongo-php-driver-legacy-master]#

12.重啓httpd (若是用的是php-fpm須要重啓/etc/init.d/php-fpm restart)

[root@centos-02 mongo-php-driver-legacy-master]# systemctl restart httpd
[root@centos-02 mongo-php-driver-legacy-master]#

1.測試,沒有出來咱們須要關閉祕鑰認證登陸

[root@centos-02 ~]# vim /var/www/html/mongo.php (若是是nginx咱們放到vim /data/wwwroot/default/mongo.php)
[root@centos-02 ~]# 
<?php
$m = new MongoClient(); // 鏈接
$db = $m->test; // 獲取名稱爲 "test" 的數據庫
$collection = $db->createCollection("runoob");
echo "集合建立成功";
?>
[root@centos-02 ~]# curl localhost/mongo.php
集合建立成功
[root@centos-02 ~]# mongo --host 192.168.133.88 --port 27017 -u admin -p 'admin122' --authenticationDatabase "admin"
MongoDB shell version v3.6.3
connecting to: mongodb://192.168.133.88:27017/
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-04-01T18:27:05.449+0800 I CONTROL  [initandlisten] 
2018-04-01T18:27:05.450+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-01T18:27:05.450+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-01T18:27:05.450+0800 I CONTROL  [initandlisten] 
> use test
switched to db test
> show tables
>

2.去掉--auth和空格,這樣mongodb就不使用用戶名密碼認證了

[root@centos-02 ~]# vim /usr/lib/systemd/system/mongod.service 
[root@centos-02 ~]# 
Environment="OPTIONS=--auth -f /etc/mongod.conf"

3.重啓mongodb

[root@centos-02 ~]# systemctl daemon-reload
[root@centos-02 ~]# systemctl restart mongod

4.再次驗證,成功

[root@centos-02 ~]# !curl
curl localhost/mongo.php
集合建立成功
[root@centos-02 ~]# mongo --host 192.168.133.88 --port 27017
MongoDB shell version v3.6.3
connecting to: mongodb://192.168.133.88:27017/
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-04-03T00:09:20.959+0800 I CONTROL  [initandlisten] 
2018-04-03T00:09:20.960+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-03T00:09:20.960+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-03T00:09:20.960+0800 I CONTROL  [initandlisten] 
2018-04-03T00:09:20.961+0800 I CONTROL  [initandlisten] 
2018-04-03T00:09:20.961+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-03T00:09:20.962+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-03T00:09:20.962+0800 I CONTROL  [initandlisten] 
> use test
switched to db test
> show tables
runoob
>

mongodb副本集介紹 

 

 mongodb副本集搭建

1.三臺機器都安裝mongodb 192.168.133.44(primary)192.168.133.88(secondary)192.168.133.66(secondary)

2.複製mongo yum源的內容到兩臺沒有安裝的機器

[root@centos-02 ~]# cd /etc/yum.repos.d/
[root@centos-02 yum.repos.d]# ls
CentOS-Base.repo       CentOS-Media.repo    epel-testing.repo            zabbix.repo.bak
CentOS-CR.repo         CentOS-Sources.repo  mongodb.repo
CentOS-Debuginfo.repo  CentOS-Vault.repo    mysql-community.repo
CentOS-fasttrack.repo  epel.repo            mysql-community-source.repo
[root@centos-02 yum.repos.d]# cat mongodb.repo 
[mongodb-org-3.6] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
[root@centos-02 yum.repos.d]#
[root@centos-01 ~]# vim /etc/yum.repos.d/mongodb.repo
[root@centos-01 ~]#
[mongodb-org-3.6] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

[root@centos-03 ~]# vim /etc/yum.repos.d/mongodb.repo
[root@centos-03 ~]# 
[mongodb-org-3.6] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc 

3.yum安裝

[root@centos-01 ~]# yum install -y mongodb-org
[root@centos-03 ~]# yum install -y mongodb-org

4.編輯配置文件

[root@centos-01 ~]# vim /etc/mongod.conf 
[root@centos-01 ~]# 
bindIp: 127.0.0.1,192.168.133.44 replication: oplogSizeMB: 20 (至關於mysql中的binlog) replSetName: mongodbslave (副本集的名字)
[root@centos-02 yum.repos.d]# vim /etc/mongod.conf 
[root@centos-02 yum.repos.d]#
  bindIp: 127.0.0.1,192.168.133.88 
replication:
  oplogSizeMB: 20
  replSetName: mongodbslave
[root@centos-03 ~]# vim /etc/mongod.conf 
[root@centos-03 ~]# 
  bindIp: 127.0.0.1,192.168.133.66
replication:
  oplogSizeMB: 20
  replSetName: mongodbsla

5.重啓mongod

[root@centos-01 ~]# systemctl restart mongod
[root@centos-01 ~]# ps aux|grep mongod
mongod     3169 22.0  4.9 1033844 49960 ?       Sl   06:54   0:02 /usr/bin/mongod -f /etc/mongod.conf
root       3198  0.0  0.0 112664   928 pts/0    S+   06:54   0:00 grep --color=auto mongod
[root@centos-01 ~]#
[root@centos-01 ~]# netstat -lntp |grep mongod
tcp        0      0 192.168.133.44:27017    0.0.0.0:*               LISTEN      3611/mongod         
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      3611/mongod         
[root@centos-01 ~]#

6.關掉三臺機器的iptables和selinux

[root@centos-01 ~]# iptables -F
[root@centos-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 7 packets, 468 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 4 packets, 448 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@centos-01 ~]#
[root@centos-01 ~]# getenforce 
Disabled
[root@centos-01 ~]#

1.進入mongo

[root@centos-01 ~]# mongo

2.配置副本集

> config={_id:"mongodblinux",members:[{_id:0,host:"192.168.133.44:27017"},{_id:1,host:"192.168.133.88:27017"},{_id:2,host:"192.168.133.66:27017"}]}
{
	"_id" : "mongodblinux",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.133.44:27017"
		},
		{
			"_id" : 1,
			"host" : "192.168.133.88:27017"
		},
		{
			"_id" : 2,
			"host" : "192.168.133.66:27017"
		}
	]
}
>

3.初始化

> rs.initiate(config)
{
	"ok" : 1,
	"operationTime" : Timestamp(1522799670, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1522799670, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mongodbslave:OTHER>

4.查看狀態

mongodbslave:OTHER> rs.status()
{
	"set" : "mongodbslave",
	"date" : ISODate("2018-04-03T23:55:50.921Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1522799710, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1522799710, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1522799710, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1522799710, 1),
			"t" : NumberLong(1)
		}
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.133.44:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 2986,
			"optime" : {
				"ts" : Timestamp(1522799710, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-04-03T23:55:10Z"),
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1522799689, 1),
			"electionDate" : ISODate("2018-04-03T23:54:49Z"),
			"configVersion" : 1,
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "192.168.133.88:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 80,
			"optime" : {
				"ts" : Timestamp(1522799710, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1522799710, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-04-03T23:55:10Z"),
			"optimeDurableDate" : ISODate("2018-04-03T23:55:10Z"),
			"lastHeartbeat" : ISODate("2018-04-03T23:55:50.564Z"),
			"lastHeartbeatRecv" : ISODate("2018-04-03T23:55:50.580Z"),
			"pingMs" : NumberLong(29),
			"syncingTo" : "192.168.133.44:27017",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.133.66:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 79,
			"optime" : {
				"ts" : Timestamp(1522799710, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1522799710, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-04-03T23:55:10Z"),
			"optimeDurableDate" : ISODate("2018-04-03T23:55:10Z"),
			"lastHeartbeat" : ISODate("2018-04-03T23:55:50.608Z"),
			"lastHeartbeatRecv" : ISODate("2018-04-03T23:55:49.460Z"),
			"pingMs" : NumberLong(23),
			"syncingTo" : "192.168.133.44:27017",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1522799710, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1522799710, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mongodbslave:PRIMARY>

mongodb副本集測試

1.建立一個庫,建立一個集合

mongodbslave:PRIMARY> use admin
switched to db admin
mongodbslave:PRIMARY> use mydb
switched to db mydb
mongodbslave:PRIMARY> db.acc.insert({AccountID:1,UserName:"123",password:"123456"})
WriteResult({ "nInserted" : 1 })
mongodbslave:PRIMARY> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
mydb    0.000GB
mongodbslave:PRIMARY> use mydb
switched to db mydb
mongodbslave:PRIMARY> show tables
acc
mongodbslave:PRIMARY>

2.從上查看mydb

[root@centos-02 ~]# mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-04-08T23:52:57.455+0800 I CONTROL  [initandlisten] 
2018-04-08T23:52:57.456+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-08T23:52:57.456+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-08T23:52:57.457+0800 I CONTROL  [initandlisten] 
2018-04-08T23:52:57.457+0800 I CONTROL  [initandlisten] 
2018-04-08T23:52:57.458+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-04-08T23:52:57.458+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-08T23:52:57.459+0800 I CONTROL  [initandlisten] 
2018-04-08T23:52:57.459+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-08T23:52:57.460+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-08T23:52:57.460+0800 I CONTROL  [initandlisten] 
mongodbslave:SECONDARY> 
mongodbslave:SECONDARY> show dbs
2018-04-09T00:10:50.623+0800 E QUERY    [thread1] Error: listDatabases failed:{
	"operationTime" : Timestamp(1523232527, 1),
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1523232527, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:816:19
shellHelper@src/mongo/shell/utils.js:706:15
@(shellhelp2):1:1
mongodbslave:SECONDARY> rs.slaveOk()
mongodbslave:SECONDARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mydb    0.000GB
mongodbslave:SECONDARY> use mydb
switched to db mydb
mongodbslave:SECONDARY> show tables
acc
mongodbslave:SECONDARY>

3.從上查看mydb

[root@centos-03 ~]# mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2018-04-03T23:07:44.471+0800 I CONTROL  [initandlisten] 
2018-04-03T23:07:44.472+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-03T23:07:44.472+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-03T23:07:44.473+0800 I CONTROL  [initandlisten] 
2018-04-03T23:07:44.555+0800 I CONTROL  [initandlisten] 
2018-04-03T23:07:44.555+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-04-03T23:07:44.555+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-03T23:07:44.589+0800 I CONTROL  [initandlisten] 
2018-04-03T23:07:44.589+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-03T23:07:44.590+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-03T23:07:44.590+0800 I CONTROL  [initandlisten] 
mongodbslave:SECONDARY> 
mongodbslave:SECONDARY> show dbs
2018-04-09T00:12:45.860+0800 E QUERY    [thread1] Error: listDatabases failed:{
	"operationTime" : Timestamp(1523232687, 1),
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1523232687, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:816:19
shellHelper@src/mongo/shell/utils.js:706:15
@(shellhelp2):1:1
mongodbslave:SECONDARY> rs.slaveOk()
mongodbslave:SECONDARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mydb    0.000GB
mongodbslave:SECONDARY> use mydb
switched to db mydb
mongodbslave:SECONDARY> show tables
acc
mongodbslave:SECONDARY>

1.查看三個節點的權重

mongodbslave:PRIMARY> rs.config()
{
	"_id" : "mongodbslave",
	"version" : 1,
	"protocolVersion" : NumberLong(1),
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.133.44:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "192.168.133.88:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "192.168.133.66:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("5ac4143306d5c156f4c42cd9")
	}
}
mongodbslave:PRIMARY>

2.模擬宕機,過了一下子發現02變成主了(由於權重都是1這個主是隨機的)

[root@centos-01 ~]# iptables -I INPUT -p tcp --dport 27017 -j DROP
[root@centos-02 ~]# mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-04-08T23:52:57.455+0800 I CONTROL  [initandlisten] 
2018-04-08T23:52:57.456+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-08T23:52:57.456+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-08T23:52:57.457+0800 I CONTROL  [initandlisten] 
2018-04-08T23:52:57.457+0800 I CONTROL  [initandlisten] 
2018-04-08T23:52:57.458+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-04-08T23:52:57.458+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-08T23:52:57.459+0800 I CONTROL  [initandlisten] 
2018-04-08T23:52:57.459+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-08T23:52:57.460+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-08T23:52:57.460+0800 I CONTROL  [initandlisten] 
mongodbslave:PRIMARY>

3.設置新的權重

[root@centos-01 ~]# iptables -D INPUT -p tcp --dport 27017 -j DROP
mongodbslave:PRIMARY> cfg=rs.conf()
{
	"_id" : "mongodbslave",
	"version" : 1,
	"protocolVersion" : NumberLong(1),
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.133.44:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "192.168.133.88:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "192.168.133.66:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("5ac4143306d5c156f4c42cd9")
	}
}
mongodbslave:PRIMARY> cfg.members[0].priority = 3
3
mongodbslave:PRIMARY> cfg.members[1].priority = 2
2
mongodbslave:PRIMARY> cfg.members[2].priority = 1
1
mongodbslave:PRIMARY> rs.reconfig(cfg)
{
	"ok" : 1,
	"operationTime" : Timestamp(1523234054, 33),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1523234054, 33),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mongodbslave:PRIMARY> rs.config()
{
	"_id" : "mongodbslave",
	"version" : 2,
	"protocolVersion" : NumberLong(1),
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.133.44:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 3,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "192.168.133.88:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 2,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "192.168.133.66:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("5ac4143306d5c156f4c42cd9")
	}
}
mongodbslave:PRIMARY>

mongodb分片介紹 

 

mongodb分片搭建

1.三臺機器分別建立mongos的日誌和其餘目錄

[root@centos-01 ~]# mkdir -p /data/mongodb/mongos/log
[root@centos-01 ~]# mkdir -p /data/mongodb/config/{data,log}
[root@centos-01 ~]# mkdir -p /data/mongodb/shard1/{data,log}
[root@centos-01 ~]# mkdir -p /data/mongodb/shard2/{data,log}
[root@centos-01 ~]# mkdir -p /data/mongodb/shard3/{data,log}
[root@centos-01 ~]#
[root@centos-02 ~]# mkdir -p /data/mongodb/mongos/log
[root@centos-02 ~]# mkdir -p /data/mongodb/config/{data,log}
[root@centos-02 ~]# mkdir -p /data/mongodb/shard1/{data,log}
[root@centos-02 ~]# mkdir -p /data/mongodb/shard2/{data,log}
[root@centos-02 ~]# mkdir -p /data/mongodb/shard3/{data,log}
[root@centos-02 ~]#
[root@centos-03 ~]# mkdir -p /data/mongodb/mongos/log
[root@centos-03 ~]# mkdir -p /data/mongodb/config/{data,log}
[root@centos-03 ~]# mkdir -p /data/mongodb/shard1/{data,log}
[root@centos-03 ~]# mkdir -p /data/mongodb/shard2/{data,log}
[root@centos-03 ~]# mkdir -p /data/mongodb/shard3/{data,log}
[root@centos-03 ~]#

1.建立配置文件的目錄

[root@centos-01 ~]# mkdir /etc/mongod/
[root@centos-01 ~]# 
[root@centos-02 ~]# mkdir /etc/mongod/
[root@centos-02 ~]#
[root@centos-03 ~]# mkdir /etc/mongod/
[root@centos-03 ~]#

2.編輯configserver的配置文件

[root@centos-01 ~]# vim /etc/mongod/config.conf
[root@centos-01 ~]# 
pidfilepath = /var/run/mongodb/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 192.168.133.44
port = 21000
fork = true
configsvr = true #declare this is a config db of a cluster;
replSet=configs #副本集名稱
maxConns=20000 #設置最大鏈接數
[root@centos-02 ~]# vim /etc/mongod/config.conf
[root@centos-02 ~]# 
pidfilepath = /var/run/mongodb/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 192.168.133.88
port = 21000
fork = true
configsvr = true #declare this is a config db of a cluster;
replSet=configs #副本集名稱
maxConns=20000 #設置最大鏈接數
[root@centos-03 ~]# vim /etc/mongod/config.conf
[root@centos-03 ~]# 
pidfilepath = /var/run/mongodb/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 192.168.133.66
port = 21000
fork = true
configsvr = true #declare this is a config db of a cluster;
replSet=configs #副本集名稱
maxConns=20000 #設置最大鏈接數

1.啓動三臺機器的config服務

[root@centos-01 ~]# mongod -f /etc/mongod/config.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 5513
child process started successfully, parent exiting
[root@centos-01 ~]# ps aux|grep mongo
mongod     1838  5.4  7.1 1602768 71468 ?       Sl   20:05   5:17 /usr/bin/mongod -f /etc/mongod.conf
root       5513 41.8  4.5 1074840 45552 ?       Sl   21:41   0:14 mongod -f /etc/mongod/config.conf
root       5553 11.0  0.0 112668   932 pts/0    S+   21:41   0:00 grep --color=auto mong
[root@centos-01 ~]#
[root@centos-01 ~]# netstat -lntp|grep mongod
tcp        0      0 192.168.133.44:21000    0.0.0.0:*               LISTEN      5513/mongod         
tcp        0      0 192.168.133.44:27017    0.0.0.0:*               LISTEN      1838/mongod         
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      1838/mongod         
[root@centos-01 ~]#
[root@centos-02 ~]# mongod -f /etc/mongod/config.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 3095
child process started successfully, parent exiting
[root@centos-02 ~]# ps aux|grep mongod
mongod     1327  3.7  9.2 1512172 92884 ?       Sl   20:17   3:19 /usr/bin/mongod -f /etc/mongod.conf
root       3095 31.1  5.0 1074480 50864 ?       Sl   21:46   0:05 mongod -f /etc/mongod/config.conf
root       3127 10.0  0.0 112684   948 pts/0    R+   21:46   0:00 grep --color=auto mongod
[root@centos-02 ~]# netstat -lntp|grep mongod
tcp        0      0 192.168.133.88:21000    0.0.0.0:*               LISTEN      3095/mongod         
tcp        0      0 192.168.133.88:27017    0.0.0.0:*               LISTEN      1327/mongod         
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      1327/mongod         
[root@centos-02 ~]#
[root@centos-03 ~]# mongod -f /etc/mongod/config.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 10596
child process started successfully, parent exiting
[root@centos-03 ~]# ps aux|grep mongod
mongod     6498  4.7  9.7 1630304 97188 ?       Sl   18:59   8:04 /usr/bin/mongod -f /etc/mongod.conf
root      10596 36.4  4.5 1071180 45568 ?       Sl   21:48   0:06 mongod -f /etc/mongod/config.conf
root      10632  0.0  0.0 112668   928 pts/0    S+   21:48   0:00 grep --color=auto mongod
[root@centos-03 ~]# netstat -lntp|grep mongod
tcp        0      0 192.168.133.66:21000    0.0.0.0:*               LISTEN      10596/mongod        
tcp        0      0 192.168.133.66:27017    0.0.0.0:*               LISTEN      6498/mongod         
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      6498/mongod         
[root@centos-03 ~]#

2.初始化副本集

[root@centos-01 ~]# mongo --host 192.168.133.44 --port 21000
MongoDB shell version v3.6.3
connecting to: mongodb://192.168.133.44:21000/
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-04-09T21:41:22.102+0800 I CONTROL  [initandlisten] 
2018-04-09T21:41:22.103+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-09T21:41:22.104+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-09T21:41:22.104+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-04-09T21:41:22.104+0800 I CONTROL  [initandlisten] 
2018-04-09T21:41:22.148+0800 I CONTROL  [initandlisten] 
2018-04-09T21:41:22.149+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-04-09T21:41:22.149+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-09T21:41:22.150+0800 I CONTROL  [initandlisten] 
2018-04-09T21:41:22.151+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-09T21:41:22.151+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-09T21:41:22.151+0800 I CONTROL  [initandlisten] 
> config = { _id: "configs", members: [ {_id : 0, host : "192.168.133.44:21000"},{_id : 1, host : "192.168.133.88:21000"},{_id : 2, host : "192.168.133.66:21000"}] }
{
	"_id" : "configs",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.133.44:21000"
		},
		{
			"_id" : 1,
			"host" : "192.168.133.88:21000"
		},
		{
			"_id" : 2,
			"host" : "192.168.133.66:21000"
		}
	]
}
> rs.initiate(config)
{
	"ok" : 1,
	"operationTime" : Timestamp(1523282429, 1),
	"$gleStats" : {
		"lastOpTime" : Timestamp(1523282429, 1),
		"electionId" : ObjectId("000000000000000000000000")
	},
	"$clusterTime" : {
		"clusterTime" : Timestamp(1523282429, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

3.查看下狀態

configs:OTHER> rs.status()
{
	"set" : "configs",
	"date" : ISODate("2018-04-09T14:02:16.668Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"configsvr" : true,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1523282518, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1523282518, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1523282518, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1523282518, 1),
			"t" : NumberLong(1)
		}
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.133.44:21000",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 1273,
			"optime" : {
				"ts" : Timestamp(1523282518, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-04-09T14:01:58Z"),
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1523282475, 1),
			"electionDate" : ISODate("2018-04-09T14:01:15Z"),
			"configVersion" : 1,
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "192.168.133.88:21000",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 106,
			"optime" : {
				"ts" : Timestamp(1523282518, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1523282518, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-04-09T14:01:58Z"),
			"optimeDurableDate" : ISODate("2018-04-09T14:01:58Z"),
			"lastHeartbeat" : ISODate("2018-04-09T14:02:16.135Z"),
			"lastHeartbeatRecv" : ISODate("2018-04-09T14:02:15.510Z"),
			"pingMs" : NumberLong(37),
			"syncingTo" : "192.168.133.44:21000",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.133.66:21000",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 106,
			"optime" : {
				"ts" : Timestamp(1523282518, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1523282518, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-04-09T14:01:58Z"),
			"optimeDurableDate" : ISODate("2018-04-09T14:01:58Z"),
			"lastHeartbeat" : ISODate("2018-04-09T14:02:14.987Z"),
			"lastHeartbeatRecv" : ISODate("2018-04-09T14:02:16.232Z"),
			"pingMs" : NumberLong(45),
			"syncingTo" : "192.168.133.44:21000",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1523282518, 1),
	"$gleStats" : {
		"lastOpTime" : Timestamp(1523282429, 1),
		"electionId" : ObjectId("7fffffff0000000000000001")
	},
	"$clusterTime" : {
		"clusterTime" : Timestamp(1523282518, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

4.下面配置mongos和三個shard

[root@centos-01 ~]# vim /etc/mongod/shard1.conf
pidfilepath = /var/run/mongodb/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
httpinterface=true #打開web監控
rest=true
replSet=shard1 #副本集名稱
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #設置最大鏈接數
[root@centos-02 ~]# vim /etc/mongod/shard1.conf
pidfilepath = /var/run/mongodb/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
httpinterface=true #打開web監控
rest=true
replSet=shard1 #副本集名稱
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #設置最大鏈接數
[root@centos-03 ~]# vim /etc/mongod/shard1.conf
pidfilepath = /var/run/mongodb/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
httpinterface=true #打開web監控
rest=true
replSet=shard1 #副本集名稱
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #設置最大鏈接數
[root@centos-01 ~]# cd /etc/mongod/
[root@centos-01 mongod]# cp shard1.conf shard2.conf 
[root@centos-01 mongod]# cp shard1.conf shard3.conf 
[root@centos-01 mongod]# sed -i 's/shard1/shard2/g' shard2.conf
[root@centos-01 mongod]# sed -i 's/shard1/shard3/g' shard3.conf
[root@centos-01 mongod]# cat !$
cat shard3.conf
pidfilepath = /var/run/mongodb/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
httpinterface=true #打開web監控
rest=true
replSet=shard3 #副本集名稱
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #設置最大鏈接數

[root@centos-01 mongod]#
[root@centos-01 mongod]# scp shard2.conf shard3.conf 192.168.133.88:/etc/mongod/
The authenticity of host '192.168.133.88 (192.168.133.88)' can't be established.
ECDSA key fingerprint is bf:dc:4b:e3:1c:99:f4:12:6f:d0:a8:e0:13:8a:d0:dd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.133.88' (ECDSA) to the list of known hosts.
root@192.168.133.88's password: 
shard2.conf                                            100%  358     0.4KB/s   00:00    
shard3.conf                                            100%  358     0.4KB/s   00:00    
[root@centos-01 mongod]# scp shard2.conf shard3.conf 192.168.133.66:/etc/mongod/
The authenticity of host '192.168.133.66 (192.168.133.66)' can't be established.
ECDSA key fingerprint is bf:dc:4b:e3:1c:99:f4:12:6f:d0:a8:e0:13:8a:d0:dd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.133.66' (ECDSA) to the list of known hosts.
root@192.168.133.66's password: 
shard2.conf                                            100%  358     0.4KB/s   00:00    
shard3.conf                                            100%  358     0.4KB/s   00:00    
[root@centos-01 mongod]#

1.啓動服務,搭建副本集(3.6版本的須要把配置文件中的httpinterface和rest註釋掉)

[root@centos-01 mongod]# mongod -f /etc/mongod/shard1.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 6581
child process started successfully, parent exiting
[root@centos-01 mongod]#
[root@centos-02 ~]# mongod -f /etc/mongod/shard1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3338
child process started successfully, parent exiting
[root@centos-02 ~]#
[root@centos-03 ~]# mongod -f /etc/mongod/shard1.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 11921
child process started successfully, parent exiting
[root@centos-03 ~]#

2.搭建shard1的副本集

> use admin
switched to db admin
> config = { _id: "shard1", members: [ {_id : 0, host : "192.168.133.44:27001"}, {_id: 1,host : "192.168.133.88:27001"},{_id : 2, host : "192.168.133.66:27001",arbiterOnly:true}] }
{
	"_id" : "shard1",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.133.44:27001"
		},
		{
			"_id" : 1,
			"host" : "192.168.133.88:27001"
		},
		{
			"_id" : 2,
			"host" : "192.168.133.66:27001",
			"arbiterOnly" : true
		}
	]
}
>
> rs.initiate(config)
{ "ok" : 1 }
shard1:OTHER> 
shard1:PRIMARY>

1.啓動shard2

[root@centos-02 ~]# mongod -f /etc/mongod/shard2.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 3554
child process started successfully, parent exiting
[root@centos-02 ~]# mongo --port 27002
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27002/
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-04-09T23:31:24.947+0800 I CONTROL  [initandlisten] 
2018-04-09T23:31:24.950+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-09T23:31:24.950+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-09T23:31:24.950+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-04-09T23:31:24.951+0800 I CONTROL  [initandlisten] 
2018-04-09T23:31:25.050+0800 I CONTROL  [initandlisten] 
2018-04-09T23:31:25.051+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-04-09T23:31:25.052+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-09T23:31:25.052+0800 I CONTROL  [initandlisten] 
2018-04-09T23:31:25.052+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-09T23:31:25.053+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-09T23:31:25.053+0800 I CONTROL  [initandlisten] 
> config = { _id: "shard2", members: [ {_id : 0, host : "192.168.133.44:27002" ,arbiterOnly:true},{_id : 1, host : "192.168.133.88:27002"},{_id : 2, host : "192.168.133.66:27002"}] }
{
	"_id" : "shard2",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.133.44:27002",
			"arbiterOnly" : true
		},
		{
			"_id" : 1,
			"host" : "192.168.133.88:27002"
		},
		{
			"_id" : 2,
			"host" : "192.168.133.66:27002"
		}
	]
}
> rs.initiate(config)
{ "ok" : 1 }
shard2:OTHER> 
shard2:PRIMARY> 

2.配置shard3

[root@centos-01 mongod]# mongod -f /etc/mongod/shard3.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 7281
[root@centos-02 ~]# mongod -f /etc/mongod/shard3.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 3653
[root@centos-03 ~]# mongod -f /etc/mongod/shard3.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 12702

1.啓動shard3

[root@centos-03 ~]# mongod -f /etc/mongod/shard3.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 12702
child process started successfully, parent exiting
[root@centos-03 ~]# 
[root@centos-03 ~]# mongo --port 27003
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27003/
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-04-09T23:38:13.623+0800 I CONTROL  [initandlisten] 
2018-04-09T23:38:13.625+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-09T23:38:13.625+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-09T23:38:13.625+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-04-09T23:38:13.626+0800 I CONTROL  [initandlisten] 
2018-04-09T23:38:13.627+0800 I CONTROL  [initandlisten] 
2018-04-09T23:38:13.627+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-04-09T23:38:13.627+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-09T23:38:13.628+0800 I CONTROL  [initandlisten] 
2018-04-09T23:38:13.628+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-09T23:38:13.629+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-09T23:38:13.629+0800 I CONTROL  [initandlisten] 
> use admin
switched to db admin
> config = { _id: "shard3", members: [ {_id : 0, host : "192.168.133.44:27003"},  {_id : 1, host : "192.168.133.88:27003", arbiterOnly:true}, {_id : 2, host : "192.168.133.66:27003"}] }
{
	"_id" : "shard3",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.133.44:27003"
		},
		{
			"_id" : 1,
			"host" : "192.168.133.88:27003",
			"arbiterOnly" : true
		},
		{
			"_id" : 2,
			"host" : "192.168.133.66:27003"
		}
	]
}
> rs.initiate(config)
{ "ok" : 1 }
shard3:OTHER> 
shard3:SECONDARY> 
shard3:SECONDARY> rs.status()
{
	"set" : "shard3",
	"date" : ISODate("2018-04-09T15:45:41.536Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1523288734, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1523288734, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1523288737, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1523288737, 1),
			"t" : NumberLong(1)
		}
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.133.44:27003",
			"health" : 1,
			"state" : 5,
			"stateStr" : "STARTUP2",
			"uptime" : 80,
			"optime" : {
				"ts" : Timestamp(1523288734, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1523288734, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-04-09T15:45:34Z"),
			"optimeDurableDate" : ISODate("2018-04-09T15:45:34Z"),
			"lastHeartbeat" : ISODate("2018-04-09T15:45:41.157Z"),
			"lastHeartbeatRecv" : ISODate("2018-04-09T15:45:40.902Z"),
			"pingMs" : NumberLong(110),
			"syncingTo" : "192.168.133.66:27003",
			"configVersion" : 1
		},
		{
			"_id" : 1,
			"name" : "192.168.133.88:27003",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",
			"uptime" : 80,
			"lastHeartbeat" : ISODate("2018-04-09T15:45:41.170Z"),
			"lastHeartbeatRecv" : ISODate("2018-04-09T15:45:36.949Z"),
			"pingMs" : NumberLong(64),
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.133.66:27003",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 519,
			"optime" : {
				"ts" : Timestamp(1523288737, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-04-09T15:45:37Z"),
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1523288730, 1),
			"electionDate" : ISODate("2018-04-09T15:45:30Z"),
			"configVersion" : 1,
			"self" : true
		}
	],
	"ok" : 1
}
shard3:PRIMARY>

1.爲何將mongos放到最後講呢,由於mongos想要起來必須先知道configserver和三個shard副本集

2.配置mongos

[root@centos-01 ~]# vim /etc/mongod/mongos.conf
[root@centos-01 ~]# 
pidfilepath = /var/run/mongodb/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
configdb = configs/192.168.133.44:21000,192.168.133.88:21000,192.168.133.66:21000 #監聽的
配置服務器,只能有1個或者3個,configs爲配置服務器的副本集名字
maxConns=20000 #設置最大鏈接數

3.三臺機器都配置mongos

[root@centos-01 ~]# scp /etc/mongod/mongos.conf 192.168.133.88:/etc/mongod/
root@192.168.133.88's password: 
mongos.conf                                            100%  364     0.4KB/s   00:00    
[root@centos-01 ~]# scp /etc/mongod/mongos.conf 192.168.133.66:/etc/mongod/
root@192.168.133.66's password: 
mongos.conf                                            100%  364     0.4KB/s   00:00    
[root@centos-01 ~]#

4.啓動mongos

 

[root@centos-01 ~]# mongos -f /etc/mongod/mongos.conf 
[root@centos-02 ~]# mongos -f /etc/mongod/mongos.conf
[root@centos-03 ~]# mongos -f /etc/mongod/mongos.conf

 

1.登陸任何一臺機器的20000端口

2.把全部分片的路由器串聯(注意IP前不要加空格)

3.查看狀態(rs.status是查看副本集,sh.status是查看分片)

mongodb分片測試 

1.登陸任意一臺20000端口

mongo --port 20000

2.執行sh.statas()

mongodb備份恢復

 

1.使用了分片了,備份的時候也須要到分片的這個端口裏面去備份,建立要備份的文件夾並執行備份命令

2.備份全部的庫

3.備份指定的集合

4.導出集合爲json文件

5.恢復數據

1.若是恢復備份的文件是json用mongoimport命令不然用mongorestore,先刪除庫

2.執行恢復操做

3.查看是否恢復成功,結果庫又恢復回來了

4.恢復指定庫

相關文章
相關標籤/搜索