NoSQL(四)
mongodb介绍
https://www.yiibai.com/mongodb/mongodb_drop_collection.html
1.文档性数据库类似于json对象,分布式
mongodb安装
1.基本思路就是创建一个官方的yum源,我们这里安装最新的3.6版本
[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有
[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.安装mongodb
[root@centos-02 yum.repos.d]# yum install -y mongodb-org
连接mongodb
1.如果想绑定多个ip访问,添加多个ip用逗号分割
bindIp: 127.0.0.1,192.168.133.88
2.启动mongodb
[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中
[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和端口
[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.如果设置了验证,需要输入用户和密码和连接的加密库
[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.恢复指定库