注意:要在两个服务器都要给其用户授权:不然报错如下
[root@test ~]# mysql -uroot -p123 -h192.168.31.131
ERROR 1045 (28000): Access denied for user 'root'@'192.168.31.131' (using password: YES)
解决办法:
mysql> grant all on *.* to root@'192.168.31.%' identified by '123';
Query OK, 0 rows affected (0.05 sec)
mysql> flush privileges
-> ;
Query OK, 0 rows affected (0.00 sec)授权之后再在192.168.31.132和192。168.31.136上写mysql.sh检测脚本
如下:
#!/bin/sh
pkill keepalived #检测资源放置脑裂
在测试随便一台服务器:[root@mysql ~]# mysql -uroot -p123 -h 192.168.31.131
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| han |
| hanye |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
停掉一台服务器在测试:
valid_lft forever preferred_lft forever
[root@mysql ~]# pkill keepalived
[root@mysql ~]# mysql -uroot -p123 -h 192.168.31.131
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 54
Server version: 5.5.46-log MySQL Community Server (GPL) by Remi
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| blog_article02 |
| db1 |
| db2 |
| db3 |
| dbtest1 |
| dbtest2 |
| dbtest3 |
| mycat |
| mysql |
| performance_schema |
| syslog |
+--------------------+
12 rows in set (0.07 sec)
注意:我这边两台服务器的数据是不一致的:更容易发现高可用成功与否!!!
[root@test ~]# ifconfig eth0 | awk -F'[: ]+' '/inet addr/{print $4}'
192.168.31.132
[root@test ~]# mysql -u root -p123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 86
Server version: 5.5.46-log MySQL Community Server (GPL) by Remi
Copyright (c) 2000, 2015, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| blog_article02 |
| db1 |
| db2 |
| db3 |
| dbtest1 |
| dbtest2 |
| dbtest3 |
| mycat |
| mysql |
| performance_schema |
| syslog |
+--------------------+
12 rows in set (0.00 sec
[root@mysql ~]# ifconfig eth0 | awk -F'[: ]+' '/inet addr/{print $4}'
192.168.31.136
[root@mysql ~]# mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 734
Server version: 5.5.32-MariaDB-log MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| han |
| hanye |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
ok了配置完成