Day01.Mysql初始化安装

[root@50 mysql]# grep password /var/log/mysqld.log
2018-09-06T03:35:28.791445Z 1 [Note] A temporary password is generated for root@localhost: <:6hlKMNfoUM

发表在 mysql | 标签为 | 留下评论

Day15.Mycat分库分表

Mycat 分库分表
IP规划
client 192.168.4.254
Mycat 192.168.4.56
c1 192.168.4.55
c2 192.168.4.54

[root@56 ~]# tar -xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[root@56 ~]# ll
总用量 985192
-rw——-. 1 root root 1838 1月 30 2018 anaconda-ks.cfg
-rw-r–r–. 1 root root 169714648 9月 17 17:10 databases2.sql
-rw-r–r–. 1 root root 1931 1月 30 2018 initial-setup-ks.cfg
drwxr-xr-x. 5 root root 4096 9月 17 16:46 lnmp_soft
-rw-r–r–. 1 root root 63305912 9月 17 16:43 lnmp_soft.tar.gz
drwxr-xr-x. 7 root root 85 9月 21 10:15 mycat
-rw-r–r–. 1 root root 15662280 9月 21 10:11 Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
drwxr-xr-x. 6 root root 68 5月 22 11:02 soft
-rw-r–r–. 1 root root 760133526 9月 13 09:36 soft.zip
drwxr-xr-x. 2 root root 6 1月 30 2018 公共
drwxr-xr-x. 2 root root 6 1月 30 2018 模板
drwxr-xr-x. 2 root root 6 1月 30 2018 视频
drwxr-xr-x. 2 root root 6 1月 30 2018 图片
drwxr-xr-x. 2 root root 6 1月 30 2018 文档
drwxr-xr-x. 2 root root 6 1月 30 2018 下载
drwxr-xr-x. 2 root root 6 1月 30 2018 音乐
drwxr-xr-x. 2 root root 6 1月 30 2018 桌面
[root@56 ~]#
[root@56 ~]#
[root@56 ~]# mv mycat/ /usr/local/
[root@56 ~]# ll /usr/local/mycat/
总用量 12
drwxr-xr-x. 2 root root 190 9月 21 10:15 bin
drwxrwxrwx. 2 root root 6 3月 1 2016 catlet
drwxrwxrwx. 4 root root 4096 9月 21 10:15 conf
drwxr-xr-x. 2 root root 4096 9月 21 10:15 lib
drwxrwxrwx. 2 root root 6 10月 28 2016 logs
-rwxrwxrwx. 1 root root 217 10月 28 2016 version.txt

发表在 mysql | 标签为 | 留下评论

Day14.Mysql视图

mysql> select * from user;
+—-+———————+——+——+——+—————————————————————–+—————————+—————-+——+———-+———-+
| id | username | pass | uid | gid | comment | homedir | shell | sex | homedir2 | pay |
+—-+———————+——+——+——+—————————————————————–+—————————+—————-+——+———-+———-+
| 1 | root | x | 0 | 0 | root | NULL | /bin/bash | boy | /123 | 5000.00 |
| 2 | bin | x | 1 | 1 | bin | /bin | /sbin/nologin | boy | /123 | 5000.00 |
| 3 | daemon | x | 2 | 2 | daemon | /sbin | /sbin/nologin | boy | /123 | 5000.00 |
| 4 | adm | x | 3 | 4 | adm | /var/adm | /sbin/nologin | boy | /123 | 5000.00 |
| 5 | lp | x | 4 | 7 | lp | /var/spool/lpd | /sbin/nologin | boy | /123 | 5000.00 |
| 6 | sync | x | 5 | 0 | sync | /sbin | /bin/sync | boy | /123 | 5000.00 |
| 7 | shutdown | x | 6 | 0 | shutdown | /sbin | /sbin/shutdown | boy | /123 | 5000.00 |
| 8 | halt | x | 7 | 0 | halt | /sbin | /sbin/halt | boy | /123 | 5000.00 |
| 9 | mail | x | 8 | 12 | mail | /var/spool/mail | /sbin/nologin | boy | /123 | 5000.00 |
| 10 | operator | x | 11 | 0 | operator | /root | /sbin/nologin | girl | /123 | 99999.99 |
| 11 | games | x | 12 | 100 | games | /usr/games | /sbin/nologin | girl | /123 | 99999.99 |
| 12 | ftp | x | 14 | 50 | FTP User | /var/ftp | /sbin/nologin | girl | /123 | 99999.99 |
| 13 | nobody | x | 99 | 99 | Nobody | / | /123456 | girl | /123 | 99999.99 |
| 14 | systemd-network | x | 192 | 192 | systemd Network Management | / | /sbin/nologin | boy | /123 | 5000.00 |
| 15 | dbus | x | 81 | 81 | System message bus | / | /123456 | girl | /123 | 99999.99 |
| 18 | rpc | x | 32 | 32 | Rpcbind Daemon | /var/lib/rpcbind | /sbin/nologin | girl | /123 | 99999.99 |
| 21 | abrt | x | 173 | 173 | | /etc/abrt | /sbin/nologin | boy | /123 | 5000.00 |
| 22 | rtkit | x | 172 | 172 | RealtimeKit | /proc | /sbin/nologin | boy | /123 | 5000.00 |
| 23 | radvd | x | 75 | 75 | radvd user | / | /123456 | girl | /123 | 99999.99 |
| 25 | tss | x | 59 | 59 | Account used by the trousers package to sandbox the tcsd daemon | /dev/null | /123456 | girl | /123 | 99999.99 |
| 26 | usbmuxd | x | 113 | 113 | usbmuxd user | / | /sbin/nologin | boy | /123 | 5000.00 |
| 28 | qemu | x | 107 | 107 | qemu user | / | /sbin/nologin | boy | /123 | 5000.00 |
| 29 | rpcuser | x | 29 | 29 | RPC Service User | /var/lib/nfs | /sbin/nologin | girl | /123 | 99999.99 |
| 32 | pulse | x | 171 | 171 | PulseAudio System Daemon | /var/run/pulse | /sbin/nologin | boy | /123 | 5000.00 |
| 33 | gdm | x | 42 | 42 | | /var/lib/gdm | /sbin/nologin | girl | /123 | 99999.99 |
| 34 | gnome-initial-setup | x | 992 | 987 | | /run/gnome-initial-setup/ | /sbin/nologin | boy | /123 | 5000.00 |
| 35 | sshd | x | 74 | 74 | Privilege-separated SSH | /var/empty/sshd | /123456 | girl | /123 | 99999.99 |
| 36 | avahi | x | 70 | 70 | Avahi mDNS/DNS-SD Stack | /var/run/avahi-daemon | /123456 | girl | /123 | 99999.99 |
| 37 | postfix | x | 89 | 89 | | /var/spool/postfix | /123456 | girl | /123 | 99999.99 |
| 38 | ntp | x | 38 | 38 | | /etc/ntp | /sbin/nologin | girl | /123 | 99999.99 |
| 39 | tcpdump | x | 72 | 72 | | / | /123456 | girl | /123 | 99999.99 |
| 41 | mysql | x | 27 | 27 | MySQL Server | /var/lib/mysql | /bin/false | girl | /123 | 99999.99 |
| 42 | zhangsan | NULL | 111 | NULL | NULL | NULL | NULL | boy | /123 | 5000.00 |
+—-+———————+——+——+——+—————————————————————–+—————————+—————-+——+———-+———-+
33 rows in set (0.00 sec)

创建视图

mysql> create view v1 as select username,pass,uid,gid from user;
Query OK, 0 rows affected (0.24 sec)

mysql> show tables;
+—————-+
| Tables_in_test |
+—————-+
| a_t |
| b_t |
| db1 |
| intab |
| name1 |
| name2 |
| name3 |
| name4 |
| name5 |
| t_a |
| t_b |
| tea |
| tea2 |
| tea4 |
| tea6 |
| tea7 |
| tea71 |
| tea8 |
| tea9 |
| user |
| v1 |
+—————-+
21 rows in set (0.00 sec)

如何知道是不是视图,看下创建结构就知道了
mysql> show create table v1;
+——+—————————————————————————————————————————————————————————————————————-+———————-+———————-+
| View | Create View | character_set_client | collation_connection |
+——+—————————————————————————————————————————————————————————————————————-+———————-+———————-+
| v1 | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `user`.`username` AS `username`,`user`.`pass` AS `pass`,`user`.`uid` AS `uid`,`user`.`gid` AS `gid` from `user` | utf8 | utf8_general_ci |
+——+—————————————————————————————————————————————————————————————————————-+———————-+———————-+
1 row in set (0.00 sec)

对视图进行操作,基表也会进行相应的操作

mysql> select * from v1;
+———————+——+——+——+
| username | pass | uid | gid |
+———————+——+——+——+
| root | x | 0 | 0 |
| bin | x | 1 | 1 |
| daemon | x | 2 | 2 |
| adm | x | 3 | 4 |
| lp | x | 4 | 7 |
| sync | x | 5 | 0 |
| shutdown | x | 6 | 0 |
| halt | x | 7 | 0 |
| mail | x | 8 | 12 |
| operator | x | 11 | 0 |
| games | x | 12 | 100 |
| ftp | x | 14 | 50 |
| nobody | x | 99 | 99 |
| systemd-network | x | 192 | 192 |
| dbus | x | 81 | 81 |
| rpc | x | 32 | 32 |
| abrt | x | 173 | 173 |
| rtkit | x | 172 | 172 |
| radvd | x | 75 | 75 |
| tss | x | 59 | 59 |
| usbmuxd | x | 113 | 113 |
| qemu | x | 107 | 107 |
| rpcuser | x | 29 | 29 |
| pulse | x | 171 | 171 |
| gdm | x | 42 | 42 |
| gnome-initial-setup | x | 992 | 987 |
| sshd | x | 74 | 74 |
| avahi | x | 70 | 70 |
| postfix | x | 89 | 89 |
| ntp | x | 38 | 38 |
| tcpdump | x | 72 | 72 |
| mysql | x | 27 | 27 |
| zhangsan | NULL | 111 | NULL |
+———————+——+——+——+
33 rows in set (0.00 sec)

修改视图表
mysql> update v1 set username=”root2″ where username=”root”;
Query OK, 1 row affected (10.36 sec)
Rows matched: 1 Changed: 1 Warnings: 0

查看基表
mysql> select username from user limit 5;
+———-+
| username |
+———-+
| root2 |
| bin |
| daemon |
| adm |
| lp |
+———-+
5 rows in set (0.01 sec)

案例:创建2张name字段相同的表
mysql> create table t1 select username,uid,shell from user limit 3;
Query OK, 3 rows affected (0.37 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> create table t2 select username,uid,homedir from user limit 3;
Query OK, 3 rows affected (0.44 sec)
Records: 3 Duplicates: 0 Warnings: 0

查询一下看看
mysql> select * from t1;
+———-+——+—————+
| username | uid | shell |
+———-+——+—————+
| root2 | 0 | /bin/bash |
| bin | 1 | /sbin/nologin |
| daemon | 2 | /sbin/nologin |
+———-+——+—————+
3 rows in set (0.00 sec)

mysql> select * from t2;
+———-+——+———+
| username | uid | homedir |
+———-+——+———+
| root2 | 0 | NULL |
| bin | 1 | /bin |
| daemon | 2 | /sbin |
+———-+——+———+
3 rows in set (0.00 sec)

正常情况下,这两张表的username字段是相同的

mysql> select * from t1,t2 where t1.username=t2.username;
+———-+——+—————+———-+——+———+
| username | uid | shell | username | uid | homedir |
+———-+——+—————+———-+——+———+
| root2 | 0 | /bin/bash | root2 | 0 | NULL |
| bin | 1 | /sbin/nologin | bin | 1 | /bin |
| daemon | 2 | /sbin/nologin | daemon | 2 | /sbin |
+———-+——+—————+———-+——+———+
3 rows in set (0.00 sec)

如果用视图直接显示,就会报错,即视图不能有相同的字段
mysql> create view v3 as select * from t1,t2 where t1.username=t2.username;
ERROR 1060 (42S21): Duplicate column name ‘username’

表名起别名,即表名字太常,直接起个简单的别名 例如 ********** from t1 a left join t2 b ,就是把t1 起名叫a t2 起名叫b
mysql> create view v3 as select a.username as anme,b.username as bname,a.uid as auid,b.uid as buid from t1 a left join t2 b on a.username=b.username;
Query OK, 0 rows affected (0.38 sec)

mysql> select * from v3;
+——–+——–+——+——+
| anme | bname | auid | buid |
+——–+——–+——+——+
| root2 | root2 | 0 | 0 |
| bin | bin | 1 | 1 |
| daemon | daemon | 2 | 2 |
+——–+——–+——+——+
3 rows in set (0.00 sec)

方法2,不用别名
mysql> create view v4(aname,bname) as select t1.username,t2.username from t1 left join t2 on t1.username=t2.username;
Query OK, 0 rows affected (0.12 sec)

mysql> select * from v4;
+——–+——–+
| aname | bname |
+——–+——–+
| root2 | root2 |
| bin | bin |
| daemon | daemon |
+——–+——–+
3 rows in set (0.00 sec)

重要选项
or replace
mysql> create view v5 as select * from t1;
Query OK, 0 rows affected (0.12 sec)

mysql> create view v5 as select * from t1;
ERROR 1050 (42S01): Table ‘v5’ already exists
mysql>
mysql> create or replace view v5 as select * from t1;
Query OK, 0 rows affected (0.10 sec)

定义处理视图的方式
具体化方式:先执行as后面的查询语句,并放入查询缓存,然后再去执行对视图表进行操作的语句。
没有指定,则默认为替换方式。没有执行as后面查询语句,并放入查询缓存的操作。

检查选项
local只检查当前视图的限制
cascaded同时检查基表和视图表的限制

案例:重新创建一个新表,记录弄少一点
mysql> create table user2 select * from user where uid>=10 and uid <=100;
Query OK, 16 rows affected (0.39 sec)
Records: 16 Duplicates: 0 Warnings: 0

mysql> select * from user2;
+—-+———-+——+——+——+—————————————————————–+———————–+—————+——+———-+———-+
| id | username | pass | uid | gid | comment | homedir | shell | sex | homedir2 | pay |
+—-+———-+——+——+——+—————————————————————–+———————–+—————+——+———-+———-+
| 10 | operator | x | 11 | 0 | operator | /root | /sbin/nologin | girl | /123 | 99999.99 |
| 11 | games | x | 12 | 100 | games | /usr/games | /sbin/nologin | girl | /123 | 99999.99 |
| 12 | ftp | x | 14 | 50 | FTP User | /var/ftp | /sbin/nologin | girl | /123 | 99999.99 |
| 13 | nobody | x | 99 | 99 | Nobody | / | /123456 | girl | /123 | 99999.99 |
| 15 | dbus | x | 81 | 81 | System message bus | / | /123456 | girl | /123 | 99999.99 |
| 18 | rpc | x | 32 | 32 | Rpcbind Daemon | /var/lib/rpcbind | /sbin/nologin | girl | /123 | 99999.99 |
| 23 | radvd | x | 75 | 75 | radvd user | / | /123456 | girl | /123 | 99999.99 |
| 25 | tss | x | 59 | 59 | Account used by the trousers package to sandbox the tcsd daemon | /dev/null | /123456 | girl | /123 | 99999.99 |
| 29 | rpcuser | x | 29 | 29 | RPC Service User | /var/lib/nfs | /sbin/nologin | girl | /123 | 99999.99 |
| 33 | gdm | x | 42 | 42 | | /var/lib/gdm | /sbin/nologin | girl | /123 | 99999.99 |
| 35 | sshd | x | 74 | 74 | Privilege-separated SSH | /var/empty/sshd | /123456 | girl | /123 | 99999.99 |
| 36 | avahi | x | 70 | 70 | Avahi mDNS/DNS-SD Stack | /var/run/avahi-daemon | /123456 | girl | /123 | 99999.99 |
| 37 | postfix | x | 89 | 89 | | /var/spool/postfix | /123456 | girl | /123 | 99999.99 |
| 38 | ntp | x | 38 | 38 | | /etc/ntp | /sbin/nologin | girl | /123 | 99999.99 |
| 39 | tcpdump | x | 72 | 72 | | / | /123456 | girl | /123 | 99999.99 |
| 41 | mysql | x | 27 | 27 | MySQL Server | /var/lib/mysql | /bin/false | girl | /123 | 99999.99 |
+—-+———-+——+——+——+—————————————————————–+———————–+—————+——+———-+———-+
16 rows in set (0.00 sec)

mysql> create view v6 as select * from user where uid<=100 with local check option;
Query OK, 0 rows affected (0.38 sec)

默认不指选项则为cascaded

mysql> select username,uid from v6;
+———-+——+
| username | uid |
+———-+——+
| root2 | 0 |
| bin | 1 |
| daemon | 2 |
| adm | 3 |
| lp | 4 |
| sync | 5 |
| shutdown | 6 |
| halt | 7 |
| mail | 8 |
| operator | 11 |
| games | 12 |
| ftp | 14 |
| nobody | 99 |
| dbus | 81 |
| rpc | 32 |
| radvd | 75 |
| tss | 59 |
| rpcuser | 29 |
| gdm | 42 |
| sshd | 74 |
| avahi | 70 |
| postfix | 89 |
| ntp | 38 |
| tcpdump | 72 |
| mysql | 27 |
+———-+——+
25 rows in set (0.00 sec)

重点:修改V6表,把uid修改为101,即大于上面的选项<=100,可以发现报错了
mysql> update v6 set uid=101 where username=”ftp”;
ERROR 1369 (HY000): CHECK OPTION failed ‘test.v6’

因为验证方式是检查<=100,所以就不满足条件

改成符合条件的uid,则可以成功
mysql> update v6 set uid=77 where username=”ftp”;
Query OK, 1 row affected (0.38 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select username,uid from v6 where uid=77;
+———-+——+
| username | uid |
+———-+——+
| ftp | 77 |
+———-+——+
1 row in set (0.00 sec)

现在验证casecaded
先创建一个视图V7,并在V7的基础上再创建V8;
mysql> create view v7 as select username,uid,shell from user2 where uid>=10 and uid<=50;
Query OK, 0 rows affected (0.35 sec)

mysql> select * from v7;
+———-+——+—————+
| username | uid | shell |
+———-+——+—————+
| operator | 11 | /sbin/nologin |
| games | 12 | /sbin/nologin |
| ftp | 14 | /sbin/nologin |
| rpc | 32 | /sbin/nologin |
| rpcuser | 29 | /sbin/nologin |
| gdm | 42 | /sbin/nologin |
| ntp | 38 | /sbin/nologin |
| mysql | 27 | /bin/false |
+———-+——+—————+
8 rows in set (0.00 sec)

mysql> create view v8 as select * from v7 where uid >=20 with cascaded check option;
Query OK, 0 rows affected (0.12 sec)

mysql> select * from v8;
+———-+——+—————+
| username | uid | shell |
+———-+——+—————+
| rpc | 32 | /sbin/nologin |
| rpcuser | 29 | /sbin/nologin |
| gdm | 42 | /sbin/nologin |
| ntp | 38 | /sbin/nologin |
| mysql | 27 | /bin/false |
+———-+——+—————+
5 rows in set (0.00 sec)

插入验证
mysql> update v8 set uid=16 where username=”rpc”;
ERROR 1369 (HY000): CHECK OPTION failed ‘test.v8’

插入16,不满足V8的条件>=20

mysql> update v8 set uid=51 where username=”rpc”;
ERROR 1369 (HY000): CHECK OPTION failed ‘test.v8’

插入51,不满足v8的基表v7的条件 uid>=10 and uid<=50

重新再插入值21,这时候就都满足条件了
mysql> update v8 set uid=21 where username=”rpc”;
Query OK, 1 row affected (0.11 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from v8;
+———-+——+—————+
| username | uid | shell |
+———-+——+—————+
| rpc | 21 | /sbin/nologin |
| rpcuser | 29 | /sbin/nologin |
| gdm | 42 | /sbin/nologin |
| ntp | 38 | /sbin/nologin |
| mysql | 27 | /bin/false |
+———-+——+—————+
5 rows in set (0.00 sec)

存储过程
数据库中保存的一系列 sql 命令的集合
编写存储过程时,可以使用变量、条件判断、流程控制等
存储过程,就是 MySQL 中的脚本

案例:把user表里第3到第9行的uid是偶数的行,显示一下。
mysql> delimiter //
mysql> create procedure p20(in x int(2),in y int(2))
-> begin
-> while x<=y do
-> select username,uid from test.user where id=x and uid%2=0;
-> set x= x+1;
-> end while;
-> end
-> //
Query OK, 0 rows affected (0.06 sec)

mysql> call p20(3,9)//
+———-+——+
| username | uid |
+———-+——+
| daemon | 2 |
+———-+——+
1 row in set (0.01 sec)

Empty set (0.01 sec)

+———-+——+
| username | uid |
+———-+——+
| lp | 4 |
+———-+——+
1 row in set (0.01 sec)

Empty set (0.01 sec)

+———-+——+
| username | uid |
+———-+——+
| shutdown | 6 |
+———-+——+
1 row in set (0.01 sec)

Empty set (0.01 sec)

+———-+——+
| username | uid |
+———-+——+
| mail | 8 |
+———-+——+
1 row in set (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

mysql>

发表在 mysql | 标签为 | 留下评论

Day13.MHA高可用配置方法

物理备份
rm -rf /var/lib/mysql/*
systemctl stop mysqld
rm -rf /var/lib/mysql
把刚才的备份文件拷贝到的当前库下
scp 192.168.4.50:/root/mysql.bak /var/lib/mysql/
重启正常

重新初始化数据库
1、停数据库服务
systemctl stop mysqld
2、删除库
rm -rf /var/lib/mysql/*
3、重新启动,mysql数据库会自己检测
[root@51 mysql]# grep password /var/log/mysqld.log | tail
2018-09-13T02:33:34.691299Z 0 [Note] Shutting down plugin ‘sha256_password’
2018-09-13T02:33:34.691305Z 0 [Note] Shutting down plugin ‘mysql_native_password’
2018-09-13T02:33:36.106363Z 0 [ERROR] unknown variable ‘validate_password_policy=0’
2018-09-13T02:33:37.992951Z 0 [Note] Shutting down plugin ‘sha256_password’
2018-09-13T02:33:37.992957Z 0 [Note] Shutting down plugin ‘mysql_native_password’
2018-09-13T02:33:41.240994Z 0 [Note] Shutting down plugin ‘sha256_password’
2018-09-13T02:33:41.240996Z 0 [Note] Shutting down plugin ‘mysql_native_password’
2018-09-13T02:48:15.931350Z 0 [Note] Shutting down plugin ‘sha256_password’
2018-09-13T02:48:15.931352Z 0 [Note] Shutting down plugin ‘mysql_native_password’
2018-09-13T02:48:27.688311Z 1 [Note] A temporary password is generated for root@localhost: p>su6#SVqixP

MHA规划
IP 规划
角色 IP 地址 主机名
Master 数据库服务器 192.168.4.51 master51
备用 1 master 数据库服务器 192.168.4.52 master52
备用 2 master 数据库服务器 192.168.4.53 master53
第 1 台 slave 服务器 192.168.4.54 slave54
第 2 台 slave 服务器 192.168.4.55 slave55
Mha_manager 服务器 192.168.4.56 mgm56
VIP 地址 192.168.4.100

第一步,安装软件包
所有主机上安装 Perl 依赖包
[root@db108 share]# ls perl-*.rpm
perl-Config-Tiny-2.14-7.el7.noarch.rpm
perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
perl-MIME-Types-1.38-2.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm
perl-Mail-Sendmail-0.79-21.el7.art.noarch.rpm
perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm
perl-MIME-Lite-3.030-1.el7.noarch.rpm

所有主机上授权root作为监控用户
[root@db108 share]# yum -y install perl-*.rpm
mysql> grant all on *.* to ‘root’@’%’ identified by
‘123456’; // 在所有数据节点上授权监控用户

代码如下
[root@51 mha-soft-student]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>
mysql>
mysql>
mysql> grant all on *.* to ‘root’@’%’ identified by ‘123456’;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
(52-55省略)

#注意#在备用master上面也要设置相同的授权复制用户
(也就是说在51、52、53都要设置repluser用户,并且要一样)
mysql> grant replication slave on *.* to repluser@”%” identified by “123456”;
Query OK, 0 rows affected, 1 warning (10.07 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.10 sec)

安装软件包
在所有数据库服务器上安装 mha-node 包
]# yum -y install perl-DBD-mysql perl-DBI
]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm

代码如下
[root@51 mha-soft-student]# yum install -y mha4mysql-node-0.56-0.el6.noarch.rpm
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在检查 mha4mysql-node-0.56-0.el6.noarch.rpm: mha4mysql-node-0.56-0.el6.noarch
mha4mysql-node-0.56-0.el6.noarch.rpm 将被安装
正在解决依赖关系
–> 正在检查事务
—> 软件包 mha4mysql-node.noarch.0.0.56-0.el6 将被 安装
–> 解决依赖关系完成

依赖关系解决

==========================================================================================================
Package 架构 版本 源 大小
==========================================================================================================
正在安装:
mha4mysql-node noarch 0.56-0.el6 /mha4mysql-node-0.56-0.el6.noarch 102 k

事务概要
==========================================================================================================
安装 1 软件包

总计:102 k
安装大小:102 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : mha4mysql-node-0.56-0.el6.noarch 1/1
验证中 : mha4mysql-node-0.56-0.el6.noarch 1/1

已安装:
mha4mysql-node.noarch 0:0.56-0.el6

完毕!
(52-55省略)

在管理主机上安装 mha_node 和 mha-manager包
]# yum -y install perl-DBD-mysql perl-DBI
]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
]# yum -y install perl-ExtUtils-* perl-CPAN-*
]#tar -zxf mha4mysql-manager-0.56.tar.gz
]#cd mha4mysql-manager-0.56
]# perl Makefile.pl
]# make
]# make install

代码如下
node的安装
[root@56 mha-soft-student]# yum install -y mha4mysql-node-0.56-0.el6.noarch.rpm
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在检查 mha4mysql-node-0.56-0.el6.noarch.rpm: mha4mysql-node-0.56-0.el6.noarch
mha4mysql-node-0.56-0.el6.noarch.rpm 将被安装
正在解决依赖关系
–> 正在检查事务
—> 软件包 mha4mysql-node.noarch.0.0.56-0.el6 将被 安装
–> 正在处理依赖关系 perl(DBD::mysql),它被软件包 mha4mysql-node-0.56-0.el6.noarch 需要
–> 正在检查事务
—> 软件包 perl-DBD-MySQL.x86_64.0.4.023-5.el7 将被 安装
–> 解决依赖关系完成

依赖关系解决

==========================================================================================================
Package 架构 版本 源 大小
==========================================================================================================
正在安装:
mha4mysql-node noarch 0.56-0.el6 /mha4mysql-node-0.56-0.el6.noarch 102 k
为依赖而安装:
perl-DBD-MySQL x86_64 4.023-5.el7 192.168.4.254_rhel7 140 k

事务概要
==========================================================================================================
安装 1 软件包 (+1 依赖软件包)

总计:242 k
总下载量:140 k
安装大小:425 k
Downloading packages:
perl-DBD-MySQL-4.023-5.el7.x86_64.rpm | 140 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : perl-DBD-MySQL-4.023-5.el7.x86_64 1/2
正在安装 : mha4mysql-node-0.56-0.el6.noarch 2/2
验证中 : perl-DBD-MySQL-4.023-5.el7.x86_64 1/2
验证中 : mha4mysql-node-0.56-0.el6.noarch 2/2

已安装:
mha4mysql-node.noarch 0:0.56-0.el6

作为依赖被安装:
perl-DBD-MySQL.x86_64 0:4.023-5.el7

完毕!

manager安装
首先安装依赖包
[root@56 mha4mysql-manager-0.56]# yum install -y perl-ExtUtils-* perl-CPAN-*
已安装:
perl-CPAN.noarch 0:1.9800-292.el7 perl-CPAN-Meta.noarch 0:2.120921-5.el7
perl-CPAN-Meta-Requirements.noarch 0:2.122-7.el7 perl-CPAN-Meta-YAML.noarch 0:0.008-14.el7
perl-ExtUtils-CBuilder.noarch 1:0.28.2.6-292.el7 perl-ExtUtils-Embed.noarch 0:1.30-292.el7
perl-ExtUtils-Install.noarch 0:1.58-292.el7 perl-ExtUtils-MakeMaker.noarch 0:6.68-3.el7
perl-ExtUtils-Manifest.noarch 0:1.61-244.el7 perl-ExtUtils-ParseXS.noarch 1:3.18-3.el7

作为依赖被安装:
gdbm-devel.x86_64 0:1.10-8.el7 glibc-devel.x86_64 0:2.17-196.el7
glibc-headers.x86_64 0:2.17-196.el7 kernel-headers.x86_64 0:3.10.0-693.el7
libdb-devel.x86_64 0:5.3.21-20.el7 perl-Digest.noarch 0:1.17-245.el7
perl-Digest-SHA.x86_64 1:5.85-4.el7 perl-IPC-Cmd.noarch 1:0.80-4.el7
perl-JSON-PP.noarch 0:2.27202-2.el7 perl-Locale-Maketext.noarch 0:1.23-3.el7
perl-Locale-Maketext-Simple.noarch 1:0.21-292.el7 perl-Module-CoreList.noarch 1:2.76.02-292.el7
perl-Module-Load.noarch 1:0.24-3.el7 perl-Module-Load-Conditional.noarch 0:0.54-3.el7
perl-Module-Metadata.noarch 0:1.000018-2.el7 perl-Params-Check.noarch 1:0.38-2.el7
perl-Parse-CPAN-Meta.noarch 1:1.4404-5.el7 perl-Perl-OSType.noarch 0:1.003-3.el7
perl-Test-Harness.noarch 0:3.28-3.el7 perl-devel.x86_64 4:5.16.3-292.el7
perl-local-lib.noarch 0:1.008010-4.el7 perl-version.x86_64 3:0.99.07-2.el7
pyparsing.noarch 0:1.5.6-9.el7 systemtap-sdt-devel.x86_64 0:3.1-3.el7

[root@56 mha4mysql-manager-0.56]# pwd
/root/soft/mysql/mha-soft-student/mha4mysql-manager-0.56
[root@56 mha4mysql-manager-0.56]# ll
总用量 44
-rw-r–r–. 1 4984 users 78 4月 1 2014 AUTHORS
drwxr-xr-x. 2 4984 users 252 4月 1 2014 bin
-rw-r–r–. 1 4984 users 17987 4月 1 2014 COPYING
drwxr-xr-x. 2 4984 users 94 4月 1 2014 debian
drwxr-xr-x. 3 4984 users 20 4月 1 2014 inc
drwxr-xr-x. 3 4984 users 17 4月 1 2014 lib
-rwxr-xr-x. 1 4984 users 846 4月 1 2014 Makefile.PL
-rw-r–r–. 1 4984 users 4875 4月 1 2014 MANIFEST
-rw-r–r–. 1 4984 users 555 4月 1 2014 META.yml
-rw-r–r–. 1 4984 users 258 4月 1 2014 README
drwxr-xr-x. 2 4984 users 35 4月 1 2014 rpm
drwxr-xr-x. 4 4984 users 33 4月 1 2014 samples
drwxr-xr-x. 2 4984 users 49 4月 1 2014 t
drwxr-xr-x. 3 4984 users 53 4月 1 2014 tests

[root@56 mha4mysql-manager-0.56]# perl Makefile.PL
*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies…
[Core Features]
– DBI …loaded. (1.627)
– DBD::mysql …loaded. (4.023)
– Time::HiRes …loaded. (1.9725)
– Config::Tiny …loaded. (2.14)
– Log::Dispatch …loaded. (2.41)
– Parallel::ForkManager …loaded. (1.18)
– MHA::NodeConst …loaded. (0.56)
*** Module::AutoInstall configuration finished.
Checking if your kit is complete…
Looks good
Writing Makefile for mha4mysql::manager
Writing MYMETA.yml and MYMETA.json
[root@56 mha4mysql-manager-0.56]# make
cp lib/MHA/ManagerUtil.pm blib/lib/MHA/ManagerUtil.pm
cp lib/MHA/Config.pm blib/lib/MHA/Config.pm
cp lib/MHA/HealthCheck.pm blib/lib/MHA/HealthCheck.pm
cp lib/MHA/ManagerConst.pm blib/lib/MHA/ManagerConst.pm
cp lib/MHA/ServerManager.pm blib/lib/MHA/ServerManager.pm
cp lib/MHA/ManagerAdmin.pm blib/lib/MHA/ManagerAdmin.pm
cp lib/MHA/FileStatus.pm blib/lib/MHA/FileStatus.pm
cp lib/MHA/ManagerAdminWrapper.pm blib/lib/MHA/ManagerAdminWrapper.pm
cp lib/MHA/MasterFailover.pm blib/lib/MHA/MasterFailover.pm
cp lib/MHA/MasterRotate.pm blib/lib/MHA/MasterRotate.pm
cp lib/MHA/MasterMonitor.pm blib/lib/MHA/MasterMonitor.pm
cp lib/MHA/SSHCheck.pm blib/lib/MHA/SSHCheck.pm
cp lib/MHA/Server.pm blib/lib/MHA/Server.pm
cp lib/MHA/DBHelper.pm blib/lib/MHA/DBHelper.pm
cp bin/masterha_stop blib/script/masterha_stop
/usr/bin/perl “-Iinc” -MExtUtils::MY -e ‘MY->fixin(shift)’ — blib/script/masterha_stop
cp bin/masterha_conf_host blib/script/masterha_conf_host
/usr/bin/perl “-Iinc” -MExtUtils::MY -e ‘MY->fixin(shift)’ — blib/script/masterha_conf_host
cp bin/masterha_check_repl blib/script/masterha_check_repl
/usr/bin/perl “-Iinc” -MExtUtils::MY -e ‘MY->fixin(shift)’ — blib/script/masterha_check_repl
cp bin/masterha_check_status blib/script/masterha_check_status
/usr/bin/perl “-Iinc” -MExtUtils::MY -e ‘MY->fixin(shift)’ — blib/script/masterha_check_status
cp bin/masterha_master_monitor blib/script/masterha_master_monitor
/usr/bin/perl “-Iinc” -MExtUtils::MY -e ‘MY->fixin(shift)’ — blib/script/masterha_master_monitor
cp bin/masterha_check_ssh blib/script/masterha_check_ssh
/usr/bin/perl “-Iinc” -MExtUtils::MY -e ‘MY->fixin(shift)’ — blib/script/masterha_check_ssh
cp bin/masterha_master_switch blib/script/masterha_master_switch
/usr/bin/perl “-Iinc” -MExtUtils::MY -e ‘MY->fixin(shift)’ — blib/script/masterha_master_switch
cp bin/masterha_secondary_check blib/script/masterha_secondary_check
/usr/bin/perl “-Iinc” -MExtUtils::MY -e ‘MY->fixin(shift)’ — blib/script/masterha_secondary_check
cp bin/masterha_manager blib/script/masterha_manager
/usr/bin/perl “-Iinc” -MExtUtils::MY -e ‘MY->fixin(shift)’ — blib/script/masterha_manager
Manifying blib/man1/masterha_stop.1
Manifying blib/man1/masterha_conf_host.1
Manifying blib/man1/masterha_check_repl.1
Manifying blib/man1/masterha_check_status.1
Manifying blib/man1/masterha_master_monitor.1
Manifying blib/man1/masterha_check_ssh.1
Manifying blib/man1/masterha_master_switch.1
Manifying blib/man1/masterha_secondary_check.1
Manifying blib/man1/masterha_manager.1
[root@56 mha4mysql-manager-0.56]# make install
Installing /usr/local/share/perl5/MHA/ManagerUtil.pm
Installing /usr/local/share/perl5/MHA/Config.pm
Installing /usr/local/share/perl5/MHA/HealthCheck.pm
Installing /usr/local/share/perl5/MHA/ManagerConst.pm
Installing /usr/local/share/perl5/MHA/ServerManager.pm
Installing /usr/local/share/perl5/MHA/ManagerAdmin.pm
Installing /usr/local/share/perl5/MHA/FileStatus.pm
Installing /usr/local/share/perl5/MHA/ManagerAdminWrapper.pm
Installing /usr/local/share/perl5/MHA/MasterFailover.pm
Installing /usr/local/share/perl5/MHA/MasterRotate.pm
Installing /usr/local/share/perl5/MHA/MasterMonitor.pm
Installing /usr/local/share/perl5/MHA/SSHCheck.pm
Installing /usr/local/share/perl5/MHA/Server.pm
Installing /usr/local/share/perl5/MHA/DBHelper.pm
Installing /usr/local/share/man/man1/masterha_stop.1
Installing /usr/local/share/man/man1/masterha_conf_host.1
Installing /usr/local/share/man/man1/masterha_check_repl.1
Installing /usr/local/share/man/man1/masterha_check_status.1
Installing /usr/local/share/man/man1/masterha_master_monitor.1
Installing /usr/local/share/man/man1/masterha_check_ssh.1
Installing /usr/local/share/man/man1/masterha_master_switch.1
Installing /usr/local/share/man/man1/masterha_secondary_check.1
Installing /usr/local/share/man/man1/masterha_manager.1
Installing /usr/local/bin/masterha_stop
Installing /usr/local/bin/masterha_conf_host
Installing /usr/local/bin/masterha_check_repl
Installing /usr/local/bin/masterha_check_status
Installing /usr/local/bin/masterha_master_monitor
Installing /usr/local/bin/masterha_check_ssh
Installing /usr/local/bin/masterha_master_switch
Installing /usr/local/bin/masterha_secondary_check
Installing /usr/local/bin/masterha_manager
Appending installation info to /usr/lib64/perl5/perllocal.pod
[root@56 mha4mysql-manager-0.56]#

至此,所有软件包安装完毕。

第二步,配置ssh密钥对认证登陆
所有数据库服务器彼此之间互相以 root 用户 ssh 秘钥对认证登录
配置管理主机以 root 用户 ssh 秘钥对认证登录所有数据节点主机
# ssh-key-gen
# ssh-copy-id [email protected]

代码如下
[root@51 mysql]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:V4KUYduUrvcT9nAUrNm9oZ4OiL+67UrKFCzcNKjcZoQ root@51
The key’s randomart image is:
+—[RSA 2048]—-+
| +o.. . |
| . . o.=. o |
| E o o o.o .+ o |
|. = + . .oo o..|
| o * + S.. .. o|
| o . . o.o +… |
| . o o +.=. |
| o o o +o. |
| o +==. .o |
+—-[SHA256]—–+

[root@51 mysql]# for i in {1..6};do ssh-copy-id 192.168.4.5$i;done
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”
The authenticity of host ‘192.168.4.51 (192.168.4.51)’ can’t be established.
ECDSA key fingerprint is SHA256:59Byua15gv4TAVEm7YutMtmgXfxGTYbzSfO84kICu0E.
ECDSA key fingerprint is MD5:7e:6e:95:ed:84:fe:5b:8e:26:94:73:9b:c3:4b:90:cb.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed — if you are prompted now it is to install the new keys
[email protected]’s password:

Number of key(s) added: 1

(52-56省略)

安装 manager 软件包 后产生的命令
命令 作用
masterha_check_ssh 检查 MHA 的 SSH 配置状况
masterha_check_repl 检查 MySQL 复制状况
masterha_manger 启动 MHA
masterha_check_status 检测 MHA 运行状态
masterha_master_monitor 检测 master 是否宕机

第三步,配置MHA集群
#################################################################################
注意:再说一遍,在主库和备用主库都要设置repluser用户,且配置要相同
mysql> grant replication slave on *.* to repluser@”%” identified by “123456”;
Query OK, 0 rows affected, 1 warning (10.07 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.10 sec)
然后,在所有的库,包括主库和从库都要设置一个监视用户admin,也要一模一样的配置
mysql> grant all on *.* to ‘root’@’%’ identified by ‘123456’;
Query OK, 0 rows affected, 1 warning (0.00 sec)
##################################################################################

(1)、配置主从同步
51上开半同步复制,配置id,bin_log日志格式mixed
[root@51 mha-soft-student]# vim /etc/my.cnf
[root@51 mha-soft-student]# systemctl restart mysqld
[root@51 mha-soft-student]# grep -v “^#” /etc/my.cnf | grep -v “^$”
[mysqld]
validate_password_policy = 0
validate_password_length = 6
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin=master51
server_id=51
binlog_format=”mixed”
plugin-load=”rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so”
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1
relay_log_purge=off

备注:解释如下
relay_log_purge=off 不自动删除本机的中继日志文件

异步复制( Asynchronous replication )
– 主库在执行完客户端提交的事务后会立即将结果返给客户端,并不关心从库是否已经接收并处理。
• 全同步复制( Fully synchronous replication )
– 当主库执行完一个事务,所有的从库都执行了该事务才返回给客户端。
• 半同步复制( Semisynchronous replication )
– 介于异步复制和全同步复制之间,主库在执行完客户端提交的事务后不是立刻返回给客户端,而是等待至少一个从库接收到并写到 relay log 中才返回给客户端

• 查看是否允许动态加载模块
– 默认允许
mysql> show variables like “have _ dynamic _ loading”;

安装完插件后,半同步复制默认是关闭的
主: mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
从: mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
查看: mysql> show variables like “rpl_semi_sync_ %_enabled”;

以下语句分别是开启主的半同步和从的半同步
plugin-load=”rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so”
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1

]# Vim /etc/my.cnf
[mysqld]
Server_id=id 号
log-bin= 日志名
Binlog_format=“mixed”
log_slave_updates

log-bin=master51 启用 binlog 日志
server_id=51 制定服务器的ID号
binlog_format=”mixed” 指定日志格式

添加主从同步授权用户
mysql> set global relay_log_purge=off;// 不自动删除本机的中继日志文件(在上面的配置文件中已经写了)
mysql> grant replication slave on *.* to repluser@”%” identified by”123456″;

代码如下
[root@51 mha-soft-student]# mysql -uroot -p123456
mysql> grant replication slave on *.* to repluser@”%” identified by “123456”;

52(备份master)上面开半同步复制,写server_id,指binlog日志名
[root@52 mha-soft-student]# grep -v “^#” /etc/my.cnf | grep -v “^$”
[mysqld]
validate_password_policy = 0
validate_password_length = 6
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server_id=52
log-bin=master52
binlog_format=”mixed”
plugin-load=”rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so”
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1
relay_log_purge=off

将该库指向51master主库
代码如下
//
先在51上确认master信息
mysql> show master status;
+—————–+———-+————–+——————+——————-+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+—————–+———-+————–+——————+——————-+
| master51.000003 | 441 | | | |
+—————–+———-+————–+——————+——————-+
1 row in set (0.00 sec)
//
[root@52 mha-soft-student]# mysql -uroot -p123456
mysql> change master to master_host=”192.168.4.51″,master_user=”repluser”,master_password=”123456″,master_log_file=”master51.000003″,master_log_pos=441;mysql> change master to master_host=”192.168.4.51″,master_user=”repluser”,master_password=”123456″,master_log_file=”master51.000003″,master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.32 sec)
mysql> start slave;
Query OK, 0 rows affected (0.07 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.51
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master51.000003
Read_Master_Log_Pos: 441
Relay_Log_File: 52-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master51.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 441
Relay_Log_Space: 523
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 51
Master_UUID: 7db35892-b6ff-11e8-a87c-52540061c690
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

53(备份master)上面开半同步复制,写server_id,指binlog日志名
[root@53 mha-soft-student]# grep -v “^#” /etc/my.cnf | grep -v “^$”
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
validate_password_policy=0
validate_password_length=6
server_id=53
log_bin=master53
binlog_format=”mixed”
plugin-load=”rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so”
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1
relay_log_purge=off

将该库指向51master主库
[root@53 mha-soft-student]# mysql -uroot -p123456

mysql> change master to master_host=”192.168.4.51″,master_user=”repluser”,master_password=”123456″,master_log_file=”master51.000003″,master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.59 sec)
mysql> start slave;
Query OK, 0 rows affected (0.05 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.51
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master51.000003
Read_Master_Log_Pos: 441
Relay_Log_File: 53-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master51.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 441
Relay_Log_Space: 523
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 51
Master_UUID: 7db35892-b6ff-11e8-a87c-52540061c690
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

54(slave)指id号就行
[root@54 mha-soft-student]# grep -v “^#” /etc/my.cnf | grep -v “^$”
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
validate_password_policy=0
validate_password_length=6
server_id=54

将54从库指向51主库
[root@54 mha-soft-student]# mysql -uroot -p123456
mysql> change master to master_host=”192.168.4.51″,master_user=”repluser”,master_password=”123456″,master_log_file=”master51.000003″,master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.81 sec)
mysql> start slave;
Query OK, 0 rows affected (0.06 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.51
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master51.000003
Read_Master_Log_Pos: 441
Relay_Log_File: 54-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master51.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 441
Relay_Log_Space: 523
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 51
Master_UUID: 7db35892-b6ff-11e8-a87c-52540061c690
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

55从库同54一样,指id即可
[root@55 mha-soft-student]# grep -v “^#” /etc/my.cnf | grep -v “^$”
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
validate_password_policy=0
validate_password_length=6
server_id=55

将55从库指向51主库
[root@55 mha-soft-student]# mysql -uroot -p123456
mysql> change master to master_host=”192.168.4.51″,master_user=”repluser”,master_password=”123456″,master_log_file=”master51.000003″,master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.50 sec)

mysql> start slave;
Query OK, 0 rows affected (0.07 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.51
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master51.000003
Read_Master_Log_Pos: 441
Relay_Log_File: 55-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master51.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 441
Relay_Log_Space: 523
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 51
Master_UUID: 7db35892-b6ff-11e8-a87c-52540061c690
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

(2)配置管理主机
管理节点主机配置文件
]# cp mha4mysql-manager-0.56/bin/* /usr/local/bin/
]#mkdir /etc/mha_manager/
[root@host114 mha4mysql-manager-0.56]# cp samples/conf/app1.cnf /etc/mha_manager/
]# vim /etc/mha_manager/app1.cnf
[server default]
manager_workdir=/etc/mha_manager
manager_log=/etc/mha_manager/manager.log
master_ip_failover_script=/usr/local/bin/master_ip_failover // 自动failover 时候的切换脚本
ssh_user=root
ssh_port=22
repl_user=repluser // 主从同步用户名
repl_password=123456 // 主从同步密码
user=root // 连接数据库服务器用户名
password=123456 // 密码配置管理主机 ( 续 1)
[server1]
hostname=192.168.4.51
port=3306
[server2]
hostname=192.168.4.52
port=3306
candidate_master=1 // 设置为候选 master
[server3]
hostname=192.168.4.53
port=3306
candidate_master=1 // 设置为候选 master配置管理主机 ( 续 2)
[server4]
hostname=192.168.4.54
port=3306
no_master=1 // 不竞选 master
[server5]
hostname=192.168.4.55
port=3306
no_master=1 // 不竞选 master

代码如下
默认manager安装时候已经把所有的配置文件拷贝到/bin目录下面了
[root@56 mha4mysql-manager-0.56]# pwd
/root/soft/mysql/mha-soft-student/mha4mysql-manager-0.56
[root@56 mha4mysql-manager-0.56]# cp bin/* /usr/local/bin/
cp:是否覆盖”/usr/local/bin/masterha_check_repl”? n
cp:是否覆盖”/usr/local/bin/masterha_check_ssh”? n
cp:是否覆盖”/usr/local/bin/masterha_check_status”? n
cp:是否覆盖”/usr/local/bin/masterha_conf_host”? n
cp:是否覆盖”/usr/local/bin/masterha_manager”? n
cp:是否覆盖”/usr/local/bin/masterha_master_monitor”? n
cp:是否覆盖”/usr/local/bin/masterha_master_switch”? n
cp:是否覆盖”/usr/local/bin/masterha_secondary_check”? n
cp:是否覆盖”/usr/local/bin/masterha_stop”? n

拷贝配置文件并编辑
[root@56 mha4mysql-manager-0.56]# mkdir /etc/mha_manager/
[root@56 mha4mysql-manager-0.56]# cp samples/conf/app1.cnf /etc/mha_manager/
[root@56 mha4mysql-manager-0.56]# ll /etc/mha_manager/
总用量 4
-rw-r–r–. 1 root root 257 9月 13 14:51 app1.cnf
[root@56 mha4mysql-manager-0.56]# vim /etc/mha_manager/app1.cnf

[root@56 mha4mysql-manager-0.56]# grep -v “^#” /etc/mha_manager/app1.cnf | grep -v “^$”
[server default]
manager_workdir=/etc/mha_manager
manager_log=/etc/mha_manager/manager.log
master_ip_failover_script=/usr/local/bin/master_ip_failover
ssh_user=root
ssh_port=22
repl_user=repluser
repl_password=123456
user=root
password=123456
[server1]
hostname=192.168.4.51
port=3306
[server2]
hostname=192.168.4.52
port=3306
candidate_master=1
[server3]
hostname=192.168.4.53
port=3306
candidate_master=1
[server4]
hostname=192.168.4.54
port=3306
no_master=1
[server5]
hostname=192.168.4.55
port=3306
no_master=1

根据配置文件,把脚本拷贝到相应的位置
[root@56 mysql]# ll
总用量 1137164
-rw-r–r–. 1 root root 3971 5月 22 11:01 master_ip_failover
-rw-r–r–. 1 root root 17333732 5月 22 11:01 maxscale-2.1.2-1.rhel.7.x86_64.rpm
drwxr-xr-x. 3 root root 4096 9月 13 11:28 mha-soft-student
-rw-r–r–. 1 root root 7663894 5月 22 10:56 Mycat-server-1.4-beta-20150604171601-linux.tar.gz
-rw-r–r–. 1 root root 569651200 5月 22 11:01 mysql-5.7.17.tar
-rw-r–r–. 1 root root 131072 5月 22 11:01 mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
-rw-r–r–. 1 7155 31415 25037548 11月 30 2016 mysql-community-client-5.7.17-1.el7.x86_64.rpm
-rw-r–r–. 1 7155 31415 277812 11月 30 2016 mysql-community-common-5.7.17-1.el7.x86_64.rpm
-rw-r–r–. 1 7155 31415 3774276 11月 30 2016 mysql-community-devel-5.7.17-1.el7.x86_64.rpm
-rw-r–r–. 1 7155 31415 45474744 11月 30 2016 mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
-rw-r–r–. 1 7155 31415 23925668 11月 30 2016 mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
-rw-r–r–. 1 7155 31415 126047424 11月 30 2016 mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
-rw-r–r–. 1 7155 31415 2237116 11月 30 2016 mysql-community-libs-5.7.17-1.el7.x86_64.rpm
-rw-r–r–. 1 7155 31415 2112724 11月 30 2016 mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
-rw-r–r–. 1 7155 31415 54571580 11月 30 2016 mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
-rw-r–r–. 1 7155 31415 169496800 11月 30 2016 mysql-community-server-5.7.17-1.el7.x86_64.rpm
-rw-r–r–. 1 7155 31415 116682212 11月 30 2016 mysql-community-test-5.7.17-1.el7.x86_64.rpm

[root@56 mysql]# cp master_ip_failover /usr/local/bin/
[root@56 mysql]# chmod +x /usr/local/bin/master_ip_failover
[root@56 mysql]# ll /usr/local/bin/master_ip_failover
-rwxr-xr-x. 1 root root 3972 9月 13 15:02 /usr/local/bin/master_ip_failover

至此配置完成

第四步,测试MHA集群
#################################################################################
注意:再说一遍,在主库和备用主库都要设置repluser用户,且配置要相同
mysql> grant replication slave on *.* to repluser@”%” identified by “123456”;
Query OK, 0 rows affected, 1 warning (10.07 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.10 sec)
然后,在所有的库,包括主库和从库都要设置一个监视用户admin,也要一模一样的配置
mysql> grant all on *.* to ‘root’@’%’ identified by ‘123456’;
Query OK, 0 rows affected, 1 warning (0.00 sec)
##################################################################################
在管理节点上 通过 master_check_ssh 做 ssh 检查
]# /usr/local/bin/masterha_check_ssh –conf= 管理节点主机主配置文件

代码如下
[root@56 mysql]# /usr/local/bin/masterha_check_ssh –conf=/etc/mha_manager/app1.cnf
Thu Sep 13 15:08:00 2018 – [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Sep 13 15:08:00 2018 – [info] Reading application default configuration from /etc/mha_manager/app1.cnf..
Thu Sep 13 15:08:00 2018 – [info] Reading server configuration from /etc/mha_manager/app1.cnf..
Thu Sep 13 15:08:00 2018 – [info] Starting SSH connection tests..
Thu Sep 13 15:08:04 2018 – [debug]
Thu Sep 13 15:08:01 2018 – [debug] Connecting via SSH from [email protected](192.168.4.52:22) to [email protected](192.168.4.51:22)..
Thu Sep 13 15:08:01 2018 – [debug] ok.
Thu Sep 13 15:08:01 2018 – [debug] Connecting via SSH from [email protected](192.168.4.52:22) to [email protected](192.168.4.53:22)..
Thu Sep 13 15:08:02 2018 – [debug] ok.
Thu Sep 13 15:08:02 2018 – [debug] Connecting via SSH from [email protected](192.168.4.52:22) to [email protected](192.168.4.54:22)..
Thu Sep 13 15:08:03 2018 – [debug] ok.
Thu Sep 13 15:08:03 2018 – [debug] Connecting via SSH from [email protected](192.168.4.52:22) to [email protected](192.168.4.55:22)..
Thu Sep 13 15:08:03 2018 – [debug] ok.
Thu Sep 13 15:08:04 2018 – [debug]
Thu Sep 13 15:08:00 2018 – [debug] Connecting via SSH from [email protected](192.168.4.51:22) to [email protected](192.168.4.52:22)..
Thu Sep 13 15:08:01 2018 – [debug] ok.
Thu Sep 13 15:08:01 2018 – [debug] Connecting via SSH from [email protected](192.168.4.51:22) to [email protected](192.168.4.53:22)..
Thu Sep 13 15:08:02 2018 – [debug] ok.
Thu Sep 13 15:08:02 2018 – [debug] Connecting via SSH from [email protected](192.168.4.51:22) to [email protected](192.168.4.54:22)..
Thu Sep 13 15:08:02 2018 – [debug] ok.
Thu Sep 13 15:08:02 2018 – [debug] Connecting via SSH from [email protected](192.168.4.51:22) to [email protected](192.168.4.55:22)..
Thu Sep 13 15:08:03 2018 – [debug] ok.
Thu Sep 13 15:08:05 2018 – [debug]
Thu Sep 13 15:08:01 2018 – [debug] Connecting via SSH from [email protected](192.168.4.53:22) to [email protected](192.168.4.51:22)..
Thu Sep 13 15:08:02 2018 – [debug] ok.
Thu Sep 13 15:08:02 2018 – [debug] Connecting via SSH from [email protected](192.168.4.53:22) to [email protected](192.168.4.52:22)..
Thu Sep 13 15:08:03 2018 – [debug] ok.
Thu Sep 13 15:08:03 2018 – [debug] Connecting via SSH from [email protected](192.168.4.53:22) to [email protected](192.168.4.54:22)..
Thu Sep 13 15:08:04 2018 – [debug] ok.
Thu Sep 13 15:08:04 2018 – [debug] Connecting via SSH from [email protected](192.168.4.53:22) to [email protected](192.168.4.55:22)..
Thu Sep 13 15:08:04 2018 – [debug] ok.
Thu Sep 13 15:08:05 2018 – [debug]
Thu Sep 13 15:08:02 2018 – [debug] Connecting via SSH from [email protected](192.168.4.54:22) to [email protected](192.168.4.51:22)..
Thu Sep 13 15:08:03 2018 – [debug] ok.
Thu Sep 13 15:08:03 2018 – [debug] Connecting via SSH from [email protected](192.168.4.54:22) to [email protected](192.168.4.52:22)..
Thu Sep 13 15:08:03 2018 – [debug] ok.
Thu Sep 13 15:08:03 2018 – [debug] Connecting via SSH from [email protected](192.168.4.54:22) to [email protected](192.168.4.53:22)..
Thu Sep 13 15:08:04 2018 – [debug] ok.
Thu Sep 13 15:08:04 2018 – [debug] Connecting via SSH from [email protected](192.168.4.54:22) to [email protected](192.168.4.55:22)..
Thu Sep 13 15:08:05 2018 – [debug] ok.
Thu Sep 13 15:08:06 2018 – [debug]
Thu Sep 13 15:08:02 2018 – [debug] Connecting via SSH from [email protected](192.168.4.55:22) to [email protected](192.168.4.51:22)..
Thu Sep 13 15:08:03 2018 – [debug] ok.
Thu Sep 13 15:08:03 2018 – [debug] Connecting via SSH from [email protected](192.168.4.55:22) to [email protected](192.168.4.52:22)..
Thu Sep 13 15:08:04 2018 – [debug] ok.
Thu Sep 13 15:08:04 2018 – [debug] Connecting via SSH from [email protected](192.168.4.55:22) to [email protected](192.168.4.53:22)..
Thu Sep 13 15:08:04 2018 – [debug] ok.
Thu Sep 13 15:08:04 2018 – [debug] Connecting via SSH from [email protected](192.168.4.55:22) to [email protected](192.168.4.54:22)..
Thu Sep 13 15:08:05 2018 – [debug] ok.
Thu Sep 13 15:08:06 2018 – [info] All SSH connection tests passed successfully.

测试主从同步状态
• 在管理节点上监控复制环境: 通过 masterha_check_repl 脚本查看整个集群的状态
]# /usr/local/bin/masterha_check_repl –conf= 管理节点主机主配置文件
#######################################################################
注意检测之前需要先把配置文件里面的脚本注释以下,避免检测出错
######################################################################

[root@56 mysql]# /usr/local/bin/masterha_check_repl –conf=/etc/mha_manager/app1.cnf
Thu Sep 13 15:40:41 2018 – [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Sep 13 15:40:41 2018 – [info] Reading application default configuration from /etc/mha_manager/app1.cnf..
Thu Sep 13 15:40:41 2018 – [info] Reading server configuration from /etc/mha_manager/app1.cnf..
Thu Sep 13 15:40:41 2018 – [info] MHA::MasterMonitor version 0.56.
Thu Sep 13 15:40:42 2018 – [info] GTID failover mode = 0
Thu Sep 13 15:40:42 2018 – [info] Dead Servers:
Thu Sep 13 15:40:42 2018 – [info] Alive Servers:
Thu Sep 13 15:40:42 2018 – [info] 192.168.4.51(192.168.4.51:3306)
Thu Sep 13 15:40:42 2018 – [info] 192.168.4.52(192.168.4.52:3306)
Thu Sep 13 15:40:42 2018 – [info] 192.168.4.53(192.168.4.53:3306)
Thu Sep 13 15:40:42 2018 – [info] 192.168.4.54(192.168.4.54:3306)
Thu Sep 13 15:40:42 2018 – [info] 192.168.4.55(192.168.4.55:3306)
Thu Sep 13 15:40:42 2018 – [info] Alive Slaves:
Thu Sep 13 15:40:42 2018 – [info] 192.168.4.52(192.168.4.52:3306) Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Sep 13 15:40:42 2018 – [info] Replicating from 192.168.4.51(192.168.4.51:3306)
Thu Sep 13 15:40:42 2018 – [info] Primary candidate for the new Master (candidate_master is set)
Thu Sep 13 15:40:42 2018 – [info] 192.168.4.53(192.168.4.53:3306) Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Sep 13 15:40:42 2018 – [info] Replicating from 192.168.4.51(192.168.4.51:3306)
Thu Sep 13 15:40:42 2018 – [info] Primary candidate for the new Master (candidate_master is set)
Thu Sep 13 15:40:42 2018 – [info] 192.168.4.54(192.168.4.54:3306) Version=5.7.17 (oldest major version between slaves) log-bin:disabled
Thu Sep 13 15:40:42 2018 – [info] Replicating from 192.168.4.51(192.168.4.51:3306)
Thu Sep 13 15:40:42 2018 – [info] Not candidate for the new Master (no_master is set)
Thu Sep 13 15:40:42 2018 – [info] 192.168.4.55(192.168.4.55:3306) Version=5.7.17 (oldest major version between slaves) log-bin:disabled
Thu Sep 13 15:40:42 2018 – [info] Replicating from 192.168.4.51(192.168.4.51:3306)
Thu Sep 13 15:40:42 2018 – [info] Not candidate for the new Master (no_master is set)
Thu Sep 13 15:40:42 2018 – [info] Current Alive Master: 192.168.4.51(192.168.4.51:3306)
Thu Sep 13 15:40:42 2018 – [info] Checking slave configurations..
Thu Sep 13 15:40:42 2018 – [info] read_only=1 is not set on slave 192.168.4.52(192.168.4.52:3306).
Thu Sep 13 15:40:42 2018 – [info] read_only=1 is not set on slave 192.168.4.53(192.168.4.53:3306).
Thu Sep 13 15:40:42 2018 – [info] read_only=1 is not set on slave 192.168.4.54(192.168.4.54:3306).
Thu Sep 13 15:40:42 2018 – [warning] relay_log_purge=0 is not set on slave 192.168.4.54(192.168.4.54:3306).
Thu Sep 13 15:40:42 2018 – [warning] log-bin is not set on slave 192.168.4.54(192.168.4.54:3306). This host cannot be a master.
Thu Sep 13 15:40:42 2018 – [info] read_only=1 is not set on slave 192.168.4.55(192.168.4.55:3306).
Thu Sep 13 15:40:42 2018 – [warning] relay_log_purge=0 is not set on slave 192.168.4.55(192.168.4.55:3306).
Thu Sep 13 15:40:42 2018 – [warning] log-bin is not set on slave 192.168.4.55(192.168.4.55:3306). This host cannot be a master.
Thu Sep 13 15:40:42 2018 – [info] Checking replication filtering settings..
Thu Sep 13 15:40:42 2018 – [info] binlog_do_db= , binlog_ignore_db=
Thu Sep 13 15:40:42 2018 – [info] Replication filtering check ok.
Thu Sep 13 15:40:42 2018 – [info] GTID (with auto-pos) is not supported
Thu Sep 13 15:40:42 2018 – [info] Starting SSH connection tests..
Thu Sep 13 15:40:48 2018 – [info] All SSH connection tests passed successfully.
Thu Sep 13 15:40:48 2018 – [info] Checking MHA Node version..
Thu Sep 13 15:40:50 2018 – [info] Version check ok.
Thu Sep 13 15:40:50 2018 – [info] Checking SSH publickey authentication settings on the current master..
Thu Sep 13 15:40:50 2018 – [info] HealthCheck: SSH to 192.168.4.51 is reachable.
Thu Sep 13 15:40:50 2018 – [info] Master MHA Node version is 0.56.
Thu Sep 13 15:40:50 2018 – [info] Checking recovery script configurations on 192.168.4.51(192.168.4.51:3306)..
Thu Sep 13 15:40:50 2018 – [info] Executing command: save_binary_logs –command=test –start_pos=4 –binlog_dir=/var/lib/mysql,/var/log/mysql –output_file=/var/tmp/save_binary_logs_test –manager_version=0.56 –start_file=master51.000003
Thu Sep 13 15:40:50 2018 – [info] Connecting to [email protected](192.168.4.51:22)..
Creating /var/tmp if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /var/lib/mysql, up to master51.000003
Thu Sep 13 15:40:51 2018 – [info] Binlog setting check done.
Thu Sep 13 15:40:51 2018 – [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Thu Sep 13 15:40:51 2018 – [info] Executing command : apply_diff_relay_logs –command=test –slave_user=’root’ –slave_host=192.168.4.52 –slave_ip=192.168.4.52 –slave_port=3306 –workdir=/var/tmp –target_version=5.7.17-log –manager_version=0.56 –relay_log_info=/var/lib/mysql/relay-log.info –relay_dir=/var/lib/mysql/ –slave_pass=xxx
Thu Sep 13 15:40:51 2018 – [info] Connecting to [email protected](192.168.4.52:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info … ok.
Relay log found at /var/lib/mysql, up to 52-relay-bin.000004
Temporary relay log file is /var/lib/mysql/52-relay-bin.000004
Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Thu Sep 13 15:40:52 2018 – [info] Executing command : apply_diff_relay_logs –command=test –slave_user=’root’ –slave_host=192.168.4.53 –slave_ip=192.168.4.53 –slave_port=3306 –workdir=/var/tmp –target_version=5.7.17-log –manager_version=0.56 –relay_log_info=/var/lib/mysql/relay-log.info –relay_dir=/var/lib/mysql/ –slave_pass=xxx
Thu Sep 13 15:40:52 2018 – [info] Connecting to [email protected](192.168.4.53:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info … ok.
Relay log found at /var/lib/mysql, up to 53-relay-bin.000002
Temporary relay log file is /var/lib/mysql/53-relay-bin.000002
Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Thu Sep 13 15:40:53 2018 – [info] Executing command : apply_diff_relay_logs –command=test –slave_user=’root’ –slave_host=192.168.4.54 –slave_ip=192.168.4.54 –slave_port=3306 –workdir=/var/tmp –target_version=5.7.17 –manager_version=0.56 –relay_log_info=/var/lib/mysql/relay-log.info –relay_dir=/var/lib/mysql/ –slave_pass=xxx
Thu Sep 13 15:40:53 2018 – [info] Connecting to [email protected](192.168.4.54:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info … ok.
Relay log found at /var/lib/mysql, up to 54-relay-bin.000002
Temporary relay log file is /var/lib/mysql/54-relay-bin.000002
Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Thu Sep 13 15:40:55 2018 – [info] Executing command : apply_diff_relay_logs –command=test –slave_user=’root’ –slave_host=192.168.4.55 –slave_ip=192.168.4.55 –slave_port=3306 –workdir=/var/tmp –target_version=5.7.17 –manager_version=0.56 –relay_log_info=/var/lib/mysql/relay-log.info –relay_dir=/var/lib/mysql/ –slave_pass=xxx
Thu Sep 13 15:40:55 2018 – [info] Connecting to [email protected](192.168.4.55:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info … ok.
Relay log found at /var/lib/mysql, up to 55-relay-bin.000002
Temporary relay log file is /var/lib/mysql/55-relay-bin.000002
Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Thu Sep 13 15:40:57 2018 – [info] Slaves settings check done.
Thu Sep 13 15:40:57 2018 – [info]
192.168.4.51(192.168.4.51:3306) (current master)
+–192.168.4.52(192.168.4.52:3306)
+–192.168.4.53(192.168.4.53:3306)
+–192.168.4.54(192.168.4.54:3306)
+–192.168.4.55(192.168.4.55:3306)

Thu Sep 13 15:40:57 2018 – [info] Checking replication health on 192.168.4.52..
Thu Sep 13 15:40:57 2018 – [info] ok.
Thu Sep 13 15:40:57 2018 – [info] Checking replication health on 192.168.4.53..
Thu Sep 13 15:40:57 2018 – [info] ok.
Thu Sep 13 15:40:57 2018 – [info] Checking replication health on 192.168.4.54..
Thu Sep 13 15:40:57 2018 – [info] ok.
Thu Sep 13 15:40:57 2018 – [info] Checking replication health on 192.168.4.55..
Thu Sep 13 15:40:57 2018 – [info] ok.
Thu Sep 13 15:40:57 2018 – [warning] master_ip_failover_script is not defined.
Thu Sep 13 15:40:57 2018 – [warning] shutdown_script is not defined.
Thu Sep 13 15:40:57 2018 – [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

第五步 启动并测试一下
开启 MHA Manager 监控
– masterha_manager // 启动命令
– –remove_dead_master_conf // 不在 app1.cnf 文件
里删除宕机的主库的信息
– –ignore_last_failover // 忽略 .health 文件
masterha_manager–conf=配置文件地址 –remove_dead_master_conf –ignore_last_failover

代码如下
[root@56 mysql]# masterha_manager –conf=/etc/mha_manager/app1.cnf –remove_dead_master_conf –ignore_last_failover
Thu Sep 13 15:53:48 2018 – [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Sep 13 15:53:48 2018 – [info] Reading application default configuration from /etc/mha_manager/app1.cnf..
Thu Sep 13 15:53:48 2018 – [info] Reading server configuration from /etc/mha_manager/app1.cnf..

查看状态 : masterha_check_status
检查 mha 服务状态:
(重新开一个终端测试)
[root@56 ~]# masterha_check_status –conf=/etc/mha_manager/app1.cnf
app1 (pid:6153) is running(0:PING_OK), master:192.168.4.51

• 停止服务 : masterha_stop
停止 mha 服务
[root@56 ~]# masterha_stop –conf=/etc/mha_manager/app1.cnf
Stopped app1 successfully.

测试高可用
修改故障切换脚本
在脚本里添加如下行
(脚本在sample里面有)
/root/soft/mysql/mha-soft-student/mha4mysql-manager-0.56/samples/scripts
[root@56 scripts]# ll
总用量 32
-rwxr-xr-x. 1 4984 users 3648 9月 13 15:00 master_ip_failover
-rwxr-xr-x. 1 4984 users 9870 9月 13 15:00 master_ip_online_change
-rwxr-xr-x. 1 4984 users 11867 4月 1 2014 power_manager
-rwxr-xr-x. 1 4984 users 1360 4月 1 2014 send_report

[root@mangle ~]# vim /usr/local/bin/master_ip_failover

$new_master_password
);
my $vip = ‘192.168.1.100/24’; # Virtual IP
my $key = “1”;
my $ssh_start_vip = “/sbin/ifconfig eth0:$key $vip”;
my $ssh_stop_vip = “/sbin/ifconfig eth0:$key down”;
GetOptions(

当主库服务器宕机后,在备用 1 主库数据库服务器上查看 VIP 地址
首先看下目前的主库时哪个,可以看到是51
[root@56 mha_manager]# masterha_check_status –conf=/etc/mha_manager/app1.cnf
app1 master is down and failover is running(50:FAILOVER_RUNNING). master:192.168.4.51
Check /etc/mha_manager/manager.log for details.

然后到51去把主库DOWN掉,模拟宕机
[root@51 mha-soft-student]# systemctl stop mysqld

再检查一下状态可以发现,已经实现自动切换到虚拟IP(我实验的时候设置的是66)
[root@56 mha_manager]# masterha_check_status –conf=/etc/mha_manager/app1.cnf
app1 master is down and failover is running(50:FAILOVER_RUNNING). master:192.168.4.51
Check /etc/mha_manager/manager.log for details.
[root@56 mha_manager]# masterha_check_status –conf=/etc/mha_manager/app1.cnf
app1 is stopped(2:NOT_RUNNING).

可以去查一下切换的日志,可以看到已经自动切换到了52
—– Failover Report —–

app1: MySQL Master failover 192.168.4.51(192.168.4.51:3306) to 192.168.4.52(192.168.4.52:3306) succeeded

Master 192.168.4.51(192.168.4.51:3306) is down!

Check MHA Manager logs at 56:/etc/mha_manager/manager.log for details.

Started automated(non-interactive) failover.
Invalidated master IP address on 192.168.4.51(192.168.4.51:3306)
The latest slave 192.168.4.52(192.168.4.52:3306) has all relay logs for recovery.
Selected 192.168.4.52(192.168.4.52:3306) as a new master.
192.168.4.52(192.168.4.52:3306): OK: Applying all logs succeeded.
192.168.4.52(192.168.4.52:3306): OK: Activated master IP address.
192.168.4.55(192.168.4.55:3306): This host has the latest relay log events.
192.168.4.54(192.168.4.54:3306): This host has the latest relay log events.
192.168.4.53(192.168.4.53:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.4.54(192.168.4.54:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.4.52(192.168.4.52:3306)
192.168.4.53(192.168.4.53:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.4.52(192.168.4.52:3306)
192.168.4.55(192.168.4.55:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.4.52(192.168.4.52:3306)
192.168.4.52(192.168.4.52:3306): Resetting slave info succeeded.
Master failover to 192.168.4.52(192.168.4.52:3306) completed successfully.

到52上去看一下,可以发现已经生成了一个虚拟IP 66(我脚本里面写的是66)
[root@52 ~]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:7a:a2:ad brd ff:ff:ff:ff:ff:ff
inet 192.168.4.52/24 brd 192.168.4.255 scope global eth0
valid_lft forever preferred_lft forever
inet 192.168.4.66/24 brd 192.168.4.255 scope global secondary eth0:1
valid_lft forever preferred_lft forever
inet6 fe80::c372:3251:9b6b:9111/64 scope link
valid_lft forever preferred_lft forever

查看一下MHA的配置文件,可以发现[server1]已经被删除了,即没有server1的信息了
[root@56 mha_manager]# grep -v “^#” /etc/mha_manager/app1.cnf | grep -v “^$”
[server default]
manager_log=/etc/mha_manager/manager.log
manager_workdir=/etc/mha_manager
master_ip_failover_script=/usr/local/bin/master_ip_failover
password=123456
repl_password=123456
repl_user=repluser
ssh_port=22
ssh_user=root
user=root
[server2]
candidate_master=1
hostname=192.168.4.52
port=3306
[server3]
candidate_master=1
hostname=192.168.4.53
port=3306
[server4]
hostname=192.168.4.54
no_master=1
port=3306
[server5]
hostname=192.168.4.55
no_master=1
port=3306

找个客户端登陆这个虚拟IP192.168.4.66,是可以正常使用的
[root@room9pc52 ~]# mysql -h 192.168.4.66 -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 41
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>

说明:
从上面的输出可以看出整个MHA的切换过程,共包括以下的步骤:
1.配置文件检查阶段,这个阶段会检查整个集群配置文件配置
2.宕机的master处理,这个阶段包括虚拟ip摘除操作,主机关机操作(这个我这里还没有实现,需要研究)
3.复制dead maste和最新slave相差的relay log,并保存到MHA Manger具体的目录下
4.识别含有最新更新的slave
5.应用从master保存的二进制日志事件(binlog events)
6.提升一个slave为新的master进行复制
7.使其他的slave连接新的master进行复制
最后启动MHA Manger监控,查看集群里面现在谁是master(在切换后监控就停止了。。。还有东西没搞对?)后来在官方网站看到这句话就明白了 。

Running MHA Manager from daemontools
Currently MHA Manager process does not run as a daemon. If failover completed successfully or the master process was killed by accident, the manager stops working. To run as a daemon, daemontool. or any external daemon program can be used. Here is an example to run from daemontools.

发表在 mysql | 标签为 | 留下评论

Day11.Mysql主从配置

1、配置主-从-从结构,并做各种测试
第一步,配置主库51
vim /etc/my.cnf

[root@51 mysql]# grep -v “^#” /etc/my.cnf | grep -v “^$”
[mysqld]
validate_password_policy = 0
validate_password_length = 6
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin=master51
server_id=51
binlog_format=”mixed”

[root@51 mysql]# systemctl restart mysqld
[root@51 mysql]# mysql -uroot -p123456
mysql> show master status;
+—————–+———-+————–+——————+——————-+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+—————–+———-+————–+——————+——————-+
| master51.000001 | 154 | | | |
+—————–+———-+————–+——————+——————-+
1 row in set (0.00 sec)

mysql> create database test1;
Query OK, 1 row affected (0.08 sec)

mysql> grant all on test1.* to test@”192.168.4.50″ identified by “123456”;
Query OK, 0 rows affected, 1 warning (0.08 sec)

第二步,配置从库52
[root@52 mysql]# vim /etc/my.cnf
[root@52 mysql]# grep -v “^#” /etc/my.cnf | grep -v “^$”
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
validate_password_policy=0
validate_password_length=6
server_id=52
log-bin=master52
binlog_format=”mixed”
log_slave_updates

[root@52 mysql]# systemctl restart mysqld
[root@52 mysql]# mysql -uroot -p123456

mysql> grant replication slave on *.* to rep2@”192.168.4.53″ identified by “123456”;

mysql> change master to master_host=’192.168.4.51′,master_user=”rep1″,master_password=”123456″,master_log_file=”master51.000001″,master_log_pos=154;

mysql> start slave;
Query OK, 0 rows affected (0.16 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.51
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master51.000001
Read_Master_Log_Pos: 154
Relay_Log_File: 52-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master51.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 523
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 51
Master_UUID: f1c08610-b633-11e8-b1ed-52540061c690
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

mysql> show master status;
+—————–+———-+————–+——————+——————-+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+—————–+———-+————–+——————+——————-+
| master52.000001 | 448 | | | |
+—————–+———-+————–+——————+——————-+
1 row in set (0.00 sec)

第3步,配置从库53
[root@53 mysql]# vim /etc/my.cnf
[root@53 mysql]# grep -v “^#” /etc/my.cnf | grep -v “^$”
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
validate_password_policy=0
validate_password_length=6
server_id=53

[root@53 mysql]# systemctl restart mysqld
[root@53 mysql]# mysql -uroot -p123456

mysql> change master to master_host=”192.168.4.52″,master_user=”rep2″,master_password=”123456″,master_log_file=”master52.000001″,master_log_pos=448;
Query OK, 0 rows affected, 2 warnings (0.53 sec)

mysql> start slave;
Query OK, 0 rows affected (0.06 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.52
Master_User: rep2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master52.000001
Read_Master_Log_Pos: 448
Relay_Log_File: 53-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master52.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 448
Relay_Log_Space: 523
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 52
Master_UUID: 3b85ac97-b635-11e8-b479-5254007aa2ad
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

第4步,做各种测试
(1)、正常测试,主库51建表,从库都能查到
主库51上面操作
mysql> create database test1;
mysql> grant all on test1.* to test@”192.168.4.50″ identified by “123456”;
客户端50远程登陆53查看
[root@50 myload]# mysql -h 192.168.4.53 -utest -p123456
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| test1 |
+——————–+
2 rows in set (0.00 sec)

(2)、从库上新建表test1,主库和另一个从库是不会复制的
接着上面53的操作
mysql> use test1;
mysql> create table d1(id int(5) unsigned auto_increment primary key,age int(2) not null default “18”,name char(10));
mysql> desc d1;
+——-+—————–+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+——-+—————–+——+—–+———+—————-+
| id | int(5) unsigned | NO | PRI | NULL | auto_increment |
| age | int(2) | NO | | 18 | |
| name | char(10) | YES | | NULL | |
+——-+—————–+——+—–+———+—————-+
3 rows in set (0.01 sec)

mysql> insert into d1(age,name) values (“22″,”lucy”);
mysql> insert into d1(age,name) values (“18″,”lily”);
mysql> exit

登陆52上面去看一眼,库里面没有表
[root@50 myload]# mysql -h 192.168.4.52 -utest -p123456
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| test1 |
+——————–+
2 rows in set (0.00 sec)

mysql> use test1;
Database changed
mysql> show tables;
Empty set (0.00 sec)

(3)、接上面的操作,在主库51上面去建表,建一个和53一模一样的表,52可以正常复制过去,53报错了
[root@50 myload]# mysql -h 192.168.4.51 -utest -p123456
mysql> show databases
-> ;
+——————–+
| Database |
+——————–+
| information_schema |
| test1 |
+——————–+
2 rows in set (0.00 sec)

mysql> use test1;
Database changed
mysql> create table d1(id int(5) unsigned auto_increment primary key,age int(2) not null default “18”,name char(10));
mysql> insert into d1(age,name) values (“22″,”lucy”);
mysql> insert into d1(age,name) values (“25″,”tom”);
mysql> exit

登陆52看一眼,是正常的
[root@50 myload]# mysql -h 192.168.4.52 -utest -p123456
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| test1 |
+——————–+
2 rows in set (0.00 sec)

mysql> use test1;
Database changed
mysql> show tables;
+—————–+
| Tables_in_test1 |
+—————–+
| d1 |
+—————–+
1 row in set (0.00 sec)

mysql> select * from d1;
+—-+—–+——+
| id | age | name |
+—-+—–+——+
| 1 | 22 | lucy |
| 2 | 25 | tom |
+—-+—–+——+
2 rows in set (0.00 sec)

登陆53发现,53的还是53自己的表,没有变化
[root@50 myload]# mysql -h 192.168.4.53 -utest -p123456

mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| test1 |
+——————–+
2 rows in set (0.00 sec)

mysql> use test1;
Database changed
mysql> show tables;
+—————–+
| Tables_in_test1 |
+—————–+
| d1 |
+—————–+
1 row in set (0.00 sec)

mysql> select * from d1;
+—-+—–+——+
| id | age | name |
+—-+—–+——+
| 1 | 22 | lucy |
| 2 | 18 | lily |
+—-+—–+——+
2 rows in set (0.00 sec)

mysql> show slave status\G;
ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER, REPLICATION CLIENT privilege(s) for this operation
ERROR:
No query specified

去53确认一下,确实报错了
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.52
Master_User: rep2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master52.000001
Read_Master_Log_Pos: 1822
Relay_Log_File: 53-relay-bin.000002
Relay_Log_Pos: 784
Relay_Master_Log_File: master52.000001
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1050
Last_Error: Error ‘Table ‘d1′ already exists’ on query. Default database: ‘test1’. Query: ‘create table d1(id int(5) unsigned auto_increment primary key,age int(2) not null default “18”,name char(10))’
Skip_Counter: 0
Exec_Master_Log_Pos: 913
Relay_Log_Space: 1897
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1050
Last_SQL_Error: Error ‘Table ‘d1′ already exists’ on query. Default database: ‘test1’. Query: ‘create table d1(id int(5) unsigned auto_increment primary key,age int(2) not null default “18”,name char(10))’
Replicate_Ignore_Server_Ids:
Master_Server_Id: 52
Master_UUID: 3b85ac97-b635-11e8-b479-5254007aa2ad
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 180912 14:20:48
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

报错之后,继续操作51,53不会同步
[root@50 myload]# mysql -h 192.168.4.51 -utest -p123456
mysql> create database test2;
ERROR 1044 (42000): Access denied for user ‘test’@’192.168.4.50’ to database ‘test2’
mysql> use test1;
mysql> create table d2(name char(20),age int(5));
mysql> insert into d2 values(“lupo”,12),(“tyu”,16);
mysql> select * from d2;
+——+——+
| name | age |
+——+——+
| lupo | 12 |
| tyu | 16 |
+——+——+
2 rows in set (0.00 sec)

mysql> exit
Bye
[root@50 myload]# mysql -h 192.168.4.53 -utest -p123456
mysql> use test1;
Database changed
mysql> show tables;
+—————–+
| Tables_in_test1 |
+—————–+
| d1 |
+—————–+
1 row in set (0.01 sec)

重新停止,配置从库53,才能正常
mysql> stop slave;
Query OK, 0 rows affected (0.07 sec)

mysql> change master to master_host=”192.168.4.52″,master_user=”rep2″,master_password=”123456″,master_log_file=”master52.000001″,master_log_pos=2300;
Query OK, 0 rows affected, 2 warnings (0.48 sec)

mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.52
Master_User: rep2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master52.000001
Read_Master_Log_Pos: 2300
Relay_Log_File: 53-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master52.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 2300
Relay_Log_Space: 523
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 52
Master_UUID: 3b85ac97-b635-11e8-b479-5254007aa2ad
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

复制模式介绍
• 异步复制( Asynchronous replication )
– 主库在执行完客户端提交的事务后会立即将结果返给客户端,并不关心从库是否已经接收并处理。
• 全同步复制( Fully synchronous replication )
– 当主库执行完一个事务,所有的从库都执行了该事务才返回给客户端。
• 半同步复制( Semisynchronous replication )
– 介于异步复制和全同步复制之间,主库在执行完客户端提交的事务后不是立刻返回给客户端,而是等待至少一个从库接收到并写到 relay log 中才返回给客户端

检查是否允许动态载入模块
mysql> show variables like “have_dynamic_loading”;
+———————-+——-+
| Variable_name | Value |
+———————-+——-+
| have_dynamic_loading | YES |
+———————-+——-+
1 row in set (0.01 sec)

发表在 mysql | 标签为 | 留下评论

创建Redis集群(重新修改)

创建Redis集群
为6台主机安装并运行redis服务
# yum -y install gcc gcc-c++
# tar -zxvf redis-4.0.8.tar.gz
# cd redis-4.0.8/
# make
# make install
# ./utils/install_server.sh
• 调整配置文件
# vim /etc/redis/redis.conf
bind IP地址 //只写物理接口IP地址
daemonize yes //守护进程方式运行
port xxxx //端口号不要使用默认的6379
cluster-enabled yes//启用集群
cluster-config-file nodes-xxxx.conf//指定集群信息文件
cluster-node-timeout 5000//请求超时 5 秒

代码如下
(以6355为例)
Hint: Its a good idea to run make test 😉

INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: 离开目录“/root/soft/redis/redis-4.0.8/src”
[root@55 redis-4.0.8]# ./utils/install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 6355
Please select the redis config file name [/etc/redis/6355.conf]
Selected default – /etc/redis/6355.conf
Please select the redis log file name [/var/log/redis_6355.log]
Selected default – /var/log/redis_6355.log
Please select the data directory for this instance [/var/lib/redis/6355]
Selected default – /var/lib/redis/6355
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6355
Config file : /etc/redis/6355.conf
Log file : /var/log/redis_6355.log
Data dir : /var/lib/redis/6355
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6355.conf => /etc/init.d/redis_6355
Installing service…
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server…
Installation successful!
[root@55 redis-4.0.8]#
[root@55 redis-4.0.8]#
[root@55 redis-4.0.8]#
[root@55 redis-4.0.8]# /etc/init.d/redis_6355 stop
Stopping …
Redis stopped
[root@55 redis-4.0.8]#
[root@55 redis-4.0.8]# vim /etc/redis/6355.conf
[root@55 redis-4.0.8]# /etc/init.d/redis_6355 start
Starting Redis server…
确认服务正常
[root@55 redis-4.0.8]# netstat -antup | grep redis
tcp 0 0 192.168.4.55:6355 0.0.0.0:* LISTEN 5351/redis-server 1
tcp 0 0 192.168.4.55:16355 0.0.0.0:* LISTEN 5351/redis-server 1
[root@55 redis-4.0.8]# /etc/init.d/redis_6355 status
Redis is running (5351)

###############################3
目前的配置文件如下
[root@55 redis-4.0.8]# grep -v “^#” /etc/redis/6355.conf | grep -v “^$”
bind 192.168.4.55
protected-mode yes
port 6355
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6355.pid
loglevel notice
logfile /var/log/redis_6355.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis/6355
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename “appendonly.aof”
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-6355.conf
cluster-node-timeout 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events “”
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
################################################

查看集群信息
[root@51 redis-4.0.8]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> CLUSTER info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.51:6351>
192.168.4.51:6351>
192.168.4.51:6351> cluster nodes
2fb277f09dee1ee10da756e2a41a7ee344c68c76 :6351@16351 myself,master – 0 0 0 connected
192.168.4.51:6351>

创建集群
(1)在51上面安装ruby环境
[root@51 redis-4.0.8]# yum install -y ruby rubygems
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
192.168.4.254_rhel7 | 4.1 kB 00:00:00
正在解决依赖关系
–> 正在检查事务
###########################################
已安装:
ruby.x86_64 0:2.0.0.648-30.el7 rubygems.noarch 0:2.0.14.1-30.el7

作为依赖被安装:
libyaml.x86_64 0:0.1.4-11.el7_0 ruby-irb.noarch 0:2.0.0.648-30.el7 ruby-libs.x86_64 0:2.0.0.648-30.el7
rubygem-bigdecimal.x86_64 0:1.2.0-30.el7 rubygem-io-console.x86_64 0:0.4.2-30.el7 rubygem-json.x86_64 0:1.7.7-30.el7
rubygem-psych.x86_64 0:2.0.0-30.el7 rubygem-rdoc.noarch 0:4.0.0-30.el7

完毕!

(2)安装ruby
[root@51 redis-cluster]# ll
总用量 204
-rw-r–r–. 1 root root 73728 5月 22 10:56 redis-3.2.1.gem
-rw-r–r–. 1 root root 131500 5月 22 10:56 ruby-devel-2.0.0.648-30.el7.x86_64.rpm
[root@51 redis-cluster]#
[root@51 redis-cluster]#
[root@51 redis-cluster]# yum -y install ruby-devel-2.0.0.648-30.el7.x86_64.rpm
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在检查 ruby-devel-2.0.0.648-30.el7.x86_64.rpm: ruby-devel-2.0.0.648-30.el7.x86_64
ruby-devel-2.0.0.648-30.el7.x86_64.rpm 将被安装
正在解决依赖关系
–> 正在检查事务
—> 软件包 ruby-devel.x86_64.0.2.0.0.648-30.el7 将被 安装
–> 解决依赖关系完成

依赖关系解决

==========================================================================================================================
Package 架构 版本 源 大小
==========================================================================================================================
正在安装:
ruby-devel x86_64 2.0.0.648-30.el7 /ruby-devel-2.0.0.648-30.el7.x86_64 337 k

事务概要
==========================================================================================================================
安装 1 软件包

总计:337 k
安装大小:337 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : ruby-devel-2.0.0.648-30.el7.x86_64 1/1
验证中 : ruby-devel-2.0.0.648-30.el7.x86_64 1/1

已安装:
ruby-devel.x86_64 0:2.0.0.648-30.el7

完毕!
[root@51 redis-cluster]#
[root@51 redis-cluster]# gem install redis-3.2.1.gem
Successfully installed redis-3.2.1
Parsing documentation for redis-3.2.1
Installing ri documentation for redis-3.2.1
1 gem installed
[root@51 redis-cluster]#

创建集群
回到redis的安装包,其内有命令文件
[root@51 src]# pwd
/root/soft/redis/redis-4.0.8/src

添加节点
# ./redis-trib.rb create –replicas 1 host:port host:port ……
–replicas 1 ,自动为每一个master节点分配一个slave节点
备注:–replicas 1 中 “1”的意思是每个主有1个从

[root@51 src]# ./redis-trib.rb create –replicas 1 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 192.168.4.54:6354 192.168.4.55:6355 192.168.4.56:6356
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes…
Using 3 masters:
192.168.4.51:6351
192.168.4.52:6352
192.168.4.53:6353
Adding replica 192.168.4.55:6355 to 192.168.4.51:6351
Adding replica 192.168.4.56:6356 to 192.168.4.52:6352
Adding replica 192.168.4.54:6354 to 192.168.4.53:6353
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
M: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
S: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
replicates c11d8dbd324bf4d733e230d1916bf2a27d3da6ad
Can I set the above configuration? (type ‘yes’ to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join..
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
S: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots: (0 slots) slave
replicates c11d8dbd324bf4d733e230d1916bf2a27d3da6ad
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

任何一台主机都能查看集群信息,例如在52上面
[root@52 redis-4.0.8]# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> CLUSTER info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:412
cluster_stats_messages_pong_sent:401
cluster_stats_messages_meet_sent:4
cluster_stats_messages_sent:817
cluster_stats_messages_ping_received:398
cluster_stats_messages_pong_received:416
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:817
192.168.4.52:6352>
192.168.4.52:6352> CLUSTER nodes
c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353@16353 master – 0 1537930177000 3 connected 10923-16383
2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351@16351 master – 0 1537930178000 1 connected 0-5460
c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352@16352 myself,master – 0 1537930178000 2 connected 5461-10922
4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354@16354 slave c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 0 1537930176467 4 connected
d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356@16356 slave c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 0 1537930176000 6 connected
dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355@16355 slave 2fb277f09dee1ee10da756e2a41a7ee344c68c76 0 1537930177000 5 connected

测试一下集群
(1)写入测试,可以看到,不同的数据分布在不同的节点
[root@51 src]# redis-cli -c -h 192.168.4.51 -p 6351
192.168.4.51:6351> set school tarena
-> Redirected to slot [8455] located at 192.168.4.52:6352
OK
192.168.4.52:6352> set class linux
OK
192.168.4.52:6352> set pay 26800
-> Redirected to slot [4013] located at 192.168.4.51:6351
OK
192.168.4.51:6351> exit
(2)读测试,取不同的值,会去对应的主机去取
[root@51 src]# redis-cli -c -h 192.168.4.55 -p 6355
192.168.4.55:6355> get name
-> Redirected to slot [5798] located at 192.168.4.52:6352
(nil)
192.168.4.52:6352> keys *
1) “school”
2) “class”
192.168.4.52:6352> get pay
-> Redirected to slot [4013] located at 192.168.4.51:6351
“26800”
192.168.4.51:6351>
(检查从节点)(看ID可以发现,56是52的从)
登陆52的从56去看一看,可以发现,值是一样的
192.168.4.51:6351> cluster nodes
4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354@16354 slave c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 0 1537931339356 4 connected
dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355@16355 slave 2fb277f09dee1ee10da756e2a41a7ee344c68c76 0 1537931339000 5 connected
2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351@16351 myself,master – 0 1537931339000 1 connected 0-5460
d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356@16356 slave c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 0 1537931340000 6 connected
c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353@16353 master – 0 1537931340000 3 connected 10923-16383
c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352@16352 master – 0 1537931338000 2 connected 5461-10922
192.168.4.51:6351> exit
[root@51 src]# redis-cli -c -h 192.168.4.56 -p 6356
192.168.4.56:6356> keys *
1) “class”
2) “school”

redis-cli命令
• 查看命令帮助
– redis-cli -h
• 常用选项
– -h IP地址
– -p 端口
– -c 集群模式redis-trib.rb脚本
• 语法格式
– redis-trib.rb 选项 参数
• 选项
– add-node 添加master主机
– check 检测集群
– reshard 重新分片
– add-node –slave 添加slave主机
– del-node 删除主机

选举master主机
• 停止master 主机的 Redis服务
– master宕机后对应的slave自动被选举为master
– 原master启动后 会自动配置为当前master的slave
• 查看集群主机信息
– redis-cli -h master_ip -p master_port
代码如下
先把52主机停止服务
[root@52 redis-4.0.8]# redis-cli -h 192.168.4.52 -p 6352 shutdown
[root@52 redis-4.0.8]# netstat -antup | grep redis
在登陆52的从56,可以看到52已经挂了,56升为新主
[root@51 src]# redis-cli -c -h 192.168.4.56 -p 6356
192.168.4.56:6356> keys *
1) “class”
2) “school”
192.168.4.56:6356> CLUSTER nodes
2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351@16351 master – 0 1537931513000 1 connected 0-5460
c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353@16353 master – 0 1537931513000 3 connected 10923-16383
4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354@16354 slave c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 0 1537931514357 4 connected
c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352@16352 master,fail – 1537931492216 1537931491000 2 disconnected
dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355@16355 slave 2fb277f09dee1ee10da756e2a41a7ee344c68c76 0 1537931513350 1 connected
d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356@16356 myself,master – 0 1537931513000 7 connected 5461-10922

修复节点
当节点修复之后,会自动变为从库(有个主机是在后面的实验加上去的,可忽略)
如下所示可以看到,52已经变成了50的从库了
[root@51 src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 4003e6be771338a6e9ed6725d0f0ba8996f72b27
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

添加master主机
• 添加master主机
– 添加时不指定主机角色,默认新主机被选为master
– # ./redis-trib.rb add-node 新主机Ip:端口 管理主机:端口
[root@51 src]# ./redis-trib.rb add-node 192.168.4.50:6350 192.168.4.51:6351
>>> Adding node 192.168.4.50:6350 to cluster 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.4.50:6350 to make it join the cluster.
[OK] New node added correctly.

检测集群主机
– ./redis-trib.rb check 192.168.4.51:6351
– 主机角色为master
– 无槽位数量
代码如下:可以看到虽然有4个主,但是新添加的50是没有槽位数的
[root@51 src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots: (0 slots) master
0 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

新添加的主机由于没有分配槽位数,所以需要重新分片
在管理节点上重新分片,本次将每个节点均摊4096
[root@51 src]# ./redis-trib.rb reshard 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots: (0 slots) master
0 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096
//问你要挪多少槽位出来,16384/4刚好等于4096
What is the receiving node ID? 4003e6be771338a6e9ed6725d0f0ba8996f72b27
//哪个节点用来接收挪出来的槽位,注意要用ID
Please enter all the source node IDs.
Type ‘all’ to use all the nodes as source nodes for the hash slots.
Type ‘done’ once you entered all the source nodes IDs.
Source node #1:all
//问你怎么挪,可以选择从单个节点,也可以选择从所有的节点
Ready to move 4096 slots.
Source nodes:
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
Destination node:
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots: (0 slots) master
0 additional replica(s)
Resharding plan:
Moving slot 5461 from d80244332c144307c2ecec60897632d81f299921
Moving slot 5462 from d80244332c144307c2ecec60897632d81f299921
Moving slot 5463 from d80244332c144307c2ecec60897632d81f299921
#################//然后它就自动开始挪了//##################################
Moving slot 12285 from 192.168.4.53:6353 to 192.168.4.50:6350:
Moving slot 12286 from 192.168.4.53:6353 to 192.168.4.50:6350:
Moving slot 12287 from 192.168.4.53:6353 to 192.168.4.50:6350:
[root@51 src]#
搞完收工,测试一下吧
[root@51 src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

添加slave主机
部署一台新redis服务器
– 装包
– 初始化
– 启用集群配置
– 运行服务
• 添加slave主机
# ./redis-trib.rb add-node –slave [ –master-id id值 ] ip地址:端口 192.168.4.51:6351
如果不指定主节点的id 的话,会把新节点 随机添加为 从节点 最少的主的从
新建了一台主机59,然后我们就把这台主机添加为从
[root@59 redis-4.0.8]# netstat -antup | grep redis
tcp 0 0 192.168.4.59:6359 0.0.0.0:* LISTEN 2985/redis-server 1
tcp 0 0 192.168.4.59:16359 0.0.0.0:* LISTEN 2985/redis-server 1

然后在51上面把59添加为从库
[root@51 src]# ./redis-trib.rb add-node –slave 192.168.4.59:6359 192.168.4.51:6351
>>> Adding node 192.168.4.59:6359 to cluster 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 4003e6be771338a6e9ed6725d0f0ba8996f72b27
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
Automatically selected master 192.168.4.56:6356
>>> Send CLUSTER MEET to node 192.168.4.59:6359 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.4.56:6356.
[OK] New node added correctly.

查看一下,因为刚才没指定是谁的从库,所以它自动把59添加为56的从。因为刚才56没有从
[root@51 src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 4003e6be771338a6e9ed6725d0f0ba8996f72b27
S: 2db7b61254b2b76315daec5f0d81d43ad625ce5e 192.168.4.59:6359
slots: (0 slots) slave
replicates d80244332c144307c2ecec60897632d81f299921
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

移除master主机
配置步骤
– 重新分片释放占用的hash槽
– 移除master主机
# redis-trib.rb reshard 192.168.4.51:6351
# redis-trib.rb del-node 192.168.4.51:6351 maste主机id值

重新分片释放占用的hash槽
– 指定移出hash槽个数
– 指定接收hash槽主机ID
– 指定移出hash槽主机ID
本次实验,我们把50主移除掉
[root@51 src]# ./redis-trib.rb reshard 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 4003e6be771338a6e9ed6725d0f0ba8996f72b27
S: 2db7b61254b2b76315daec5f0d81d43ad625ce5e 192.168.4.59:6359
slots: (0 slots) slave
replicates d80244332c144307c2ecec60897632d81f299921
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096
//问你要移除多少个槽位,因为50有4096个,所以我们输入4096
What is the receiving node ID? 2fb277f09dee1ee10da756e2a41a7ee344c68c76
//问你哪个来接受,就先指定主51吧,注意都是写ID值
Please enter all the source node IDs.
Type ‘all’ to use all the nodes as source nodes for the hash slots.
Type ‘done’ once you entered all the source nodes IDs.
Source node #1:4003e6be771338a6e9ed6725d0f0ba8996f72b27
Source node #2:done
//问你从哪里移除,注意我们这一次是删除主50,所以就把50的ID写上去
Ready to move 4096 slots.
Source nodes:
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
Destination node:
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 0 from 4003e6be771338a6e9ed6725d0f0ba8996f72b27
Moving slot 1 from 4003e6be771338a6e9ed6725d0f0ba8996f72b27
Moving slot 2 from 4003e6be771338a6e9ed6725d0f0ba8996f72b27
########################然后他就开始移除了,要等会###############################
Moving slot 12285 from 192.168.4.50:6350 to 192.168.4.51:6351:
Moving slot 12286 from 192.168.4.50:6350 to 192.168.4.51:6351:
Moving slot 12287 from 192.168.4.50:6350 to 192.168.4.51:6351:
//搞完收工,查看确认一下
[root@51 src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-6826,10923-12287 (8192 slots) master
2 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots: (0 slots) master
0 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
S: 2db7b61254b2b76315daec5f0d81d43ad625ce5e 192.168.4.59:6359
slots: (0 slots) slave
replicates d80244332c144307c2ecec60897632d81f299921
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
已经移除完毕,现在删除主50
[root@51 src]# ./redis-trib.rb del-node 192.168.4.51:6351 4003e6be771338a6e9ed6725d0f0ba8996f72b27
>>> Removing node 4003e6be771338a6e9ed6725d0f0ba8996f72b27 from cluster 192.168.4.51:6351
>>> Sending CLUSTER FORGET messages to the cluster…
>>> SHUTDOWN the node.
[root@51 src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-6826,10923-12287 (8192 slots) master
2 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
S: 2db7b61254b2b76315daec5f0d81d43ad625ce5e 192.168.4.59:6359
slots: (0 slots) slave
replicates d80244332c144307c2ecec60897632d81f299921
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
[root@51 src]#

现在删除从库52,删除从非常简单,直接删除
[root@51 src]# ./redis-trib.rb del-node 192.168.4.51:6351 c11d8dbd324bf4d733e230d1916bf2a27d3da6ad
>>> Removing node c11d8dbd324bf4d733e230d1916bf2a27d3da6ad from cluster 192.168.4.51:6351
>>> Sending CLUSTER FORGET messages to the cluster…
>>> SHUTDOWN the node.
[root@51 src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-6826,10923-12287 (8192 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: 2db7b61254b2b76315daec5f0d81d43ad625ce5e 192.168.4.59:6359
slots: (0 slots) slave
replicates d80244332c144307c2ecec60897632d81f299921
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

redis-trib.rb脚本选项总结
• redis-trib.rb 常用选项
create 创建集群
check 检查集群
reshard 重新分片
del-node 删除主机
add-node –slave 添加slave主机
add-node 添加master主机

发表在 reids | 标签为 | 留下评论

Redis绑定IP后报 bind: Cannot assign requested address 异常解决方法

Redis绑定IP后报 bind: Cannot assign requested address 异常解决方法

今天在研究Redis安全设置,提到bind IP地址,然后就在redis.conf文件中的bind项后加入了客户端的IP,以为是绑定了IP的机器才能访问Redis,修改完成后重启,报错了:

Creating Server TCP listening socket 192.168.1.55:6379: bind: Cannot assign requested address

提示监听192.168.1.55地址的6379端口错误,无法分配请求地址!

报着疑问,百度了一下,发现原来是我理解错了bind关键字的意义,原来的作用是绑定本机IP和地址

这个是知乎上的解答:http://www.zhihu.com/question/27220050/answer/38463570

redis.conf中的原文解释:

# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
发表在 reids | 标签为 | 留下评论

安装redis make报错 zmalloc.h:50:31: 错误:jemalloc/jemalloc.h:没有那个文件或目录

原因分析:

在redis的解压包下有个README文件,打开这个文件 有这个一段话。

llocator
———

Selecting a non-default memory allocator when building Redis is done by setting
the `MALLOC` environment variable. Redis is compiled and linked against libc
malloc by default, with the exception of jemalloc being the default on Linux
systems. This default was picked because jemalloc has proven to have fewer
fragmentation problems than libc malloc.

To force compiling against libc malloc, use:

% make MALLOC=libc

To compile against jemalloc on Mac OS X systems, use:

% make MALLOC=jemalloc

Verbose build
————-

说的是关于分配器allocator, 如果有MALLOC  这个 环境变量, 会有用这个环境变量的 去建立Redis。

而且libc 并不是默认的 分配器, 默认的是 jemalloc, 因为 jemalloc 被证明 有更少的 fragmentation problems 比libc。

但是如果你又没有jemalloc 而只有 libc 当然 make 出错。 所以加这么一个参数。

解决办法

make MALLOC=libc

发表在 reids | 标签为 | 留下评论

Linux查找包名

Linux查找包名

一、rpm包安装的,可以用rpm -qa看到,如果要查找某软件包是否安装,用 rpm -qa | grep “软件或者包的名字” 。

[root@fencatn ~] rpm -qa | grep ruby

二、以deb包安装的,可以用dpkg -l能看到。如果是查找指定软件包,用dpkg -l | grep “软件或者包的名字” ;

[root@fencatn ~]dpkg-l|grepruby

三、yum方法安装的,可以用yum list installed查找,如果是查找指定包,命令后加 | grep “软件名或者包名” ;

[root@fencatn ~] yum list installed | grep ruby

四、如果是以源码包自己编译安装的,例如.tar.gz或者tar.bz2形式的,这个只能看可执行文件是否存在了,

上面两种方法都看不到这种源码形式安装的包。如果是以root用户安装的,可执行程序通常都在/sbin:/usr/bin目录下。

说明:其中rpm yum 是Redhat系linux的软件包管理命令,dpkg是debian系列的软件包管理命令

发表在 LinuxBasic | 标签为 , | 留下评论

yum 根据命令查找包名

yum 根据命令查找包名

[root@fencatn ~]# yum whatprovides xeyes
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
xorg-x11-apps-7.7-7.el7.x86_64 : X.Org X11 applications
源 :cos7
匹配来源:
提供 :xeyes

xorg-x11-apps-7.7-7.el7.x86_64 : X.Org X11 applications
源 :@cos7
匹配来源:
提供 :xeyes

发表在 LinuxBasic | 标签为 | 留下评论