#10登录数据库 [root@ztmonitor01 softs]# mysql -uroot -p123456 #下面的%表示允许任何ip访问此mysql数据库 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; mysql> flush privileges; mysql> exit;
#10登录数据库 [root@ztmonitor02 ~]# mysql -uroot -p123456
#下面的%表示允许任何ip访问此mysql数据库 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; mysql> flush privileges; mysql> exit;
2.1.3 确认两数据库实例id
如下可以看到两个数据库实例的id是不同的。
1 2 3 4 5 6 7 8
#节点1 [root@ztmonitor01 ~]# mysql -uroot -p123456 -e "select @@server_id" mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 1 | +-------------+
1 2 3 4 5 6 7 8
#节点2 [root@ztmonitor02 ~]# mysql -uroot -p123456 -e "select @@server_id" mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 2 | +-------------+
[root@ztmonitor01 ~]# mysql -uroot -p123456 -e "show variables like '%log_bin%';" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------------------------+-----------------------------+ | Variable_name | Value | +---------------------------------+-----------------------------+ | log_bin | ON | | log_bin_basename | /data/mysql/mysql-bin | | log_bin_index | /data/mysql/mysql-bin.index | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | | sql_log_bin | ON | +---------------------------------+-----------------------------+
1 2 3 4 5 6 7 8 9 10 11 12
[root@ztmonitor02 ~]# mysql -uroot -p123456 -e "show variables like '%log_bin%';" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------------------------+-----------------------------+ | Variable_name | Value | +---------------------------------+-----------------------------+ | log_bin | ON | | log_bin_basename | /data/mysql/mysql-bin | | log_bin_index | /data/mysql/mysql-bin.index | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | | sql_log_bin | ON | +---------------------------------+-----------------------------+
#创建用户replication,密码是replication,此用户能从任何ip登录进主库,对所有数据库的所有成员拥有replication slave权限 mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO replication@'%' IDENTIFIED BY 'replication'; mysql> flush privileges; mysql> exit;
2.2.3 在节点1上执行同步操作
首先在节点2上查询其上的数据库实例状态信息。
1
mysql> show master status;
image-20240810231040169
记住此File列与Position列的值mysql-bin.000002、1327
以下命令在节点1上执行(节点1数据库实例的主库是节点2上的数据库实例)
1 2 3 4
mysql> change master to master_host='172.20.180.165',master_port=3306,master_user='replication',master_password='replication',master_log_file='mysql-bin.000002',master_log_pos=1327; mysql> start slave; mysql> show slave status\G mysql> exit;
image-20240810231319922image-20240810231352704
2.2.4 在节点2上执行同步操作
首先在节点1上查询其上的数据库实例状态信息。
1
mysql> show master status;
image-20240810231523835
记住此File列与Position列的值mysql-bin.000002、1327
以下命令在节点2上执行(节点2数据库实例的主库是节点1上的数据库实例)
1 2 3 4
mysql> change master to master_host='172.20.180.44',master_port=3306,master_user='replication',master_password='replication',master_log_file='mysql-bin.000002',master_log_pos=1327; mysql> start slave; mysql> show slave status\G mysql> exit;
image-20240810231640870
2.2.5 验证互为主从数据库生效
在节点1上的MySQL数据库实例中创建数据库db01。
1
[root@ztmonitor01 ~]# mysql -uroot -p123456 -e "create database db01;"
image-20240810231810961
在节点2上的MySQL数据库实例中创建数据库db02。
1
[root@ztmonitor02 ~]# mysql -uroot -p123456 -e "create database db02;"
################################################## #File Name : mycheck.sh #Description: mysql is working MYSQL_OK is 1 # mysql is down MYSQL_OK is 0 ##################################################
BASEPATH=/data/server/mysql LOGSPATH=$BASEPATH/logs source$BASEPATH/.mysqlenv if [ ! -d $LOGSPATH ];then mkdir -p $LOGSPATH fi
CHECK_TIME=3 MYSQL_OK=1 ################################################################## functioncheck_mysql_helth (){ $mysql -e "show status;" >/dev/null 2>&1 if [ $? == 0 ] then MYSQL_OK=1 else MYSQL_OK=0 #systemctl status keepalived fi return$MYSQL_OK }
#check_mysql_helth while [ $CHECK_TIME -ne 0 ] #不等于 do let"CHECK_TIME -= 1" check_mysql_helth if [ $MYSQL_OK = 1 ];then CHECK_TIME=0 echo"$(date "+%Y-%m-%d %H:%M:%S") The scripts mycheck.sh is running ..." >> $LOGSPATH/mysql_switch.log exit 0 fi if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ] then echo"$(date "+%Y-%m-%d %H:%M:%S") The mycheck.sh, mysql is down, after switch..." >> $LOGSPATH/mysql_switch.log systemctl stop keepalived exit 1 fi sleep 1 done
################################################## #File Name : mymaster.sh #Description: First determine whether synchronous # replication is performed, and if no # execution is completed, wait for 1 # minutes. Log logs and POS after # switching, and record files synchronously. ##################################################
BASEPATH=/data/server/mysql LOGSPATH=$BASEPATH/logs source$BASEPATH/.mysqlenv if [ ! -d $LOGSPATH ];then mkdir$LOGSPATH fi
################################################## #File Name : mybackup.sh #Description: Empty the slave configuration, retrieve # the remote log file and Pos, and open # the synchronization ##################################################
BASEPATH=/data/server/mysql LOGSPATH=$BASEPATH/logs source$BASEPATH/.mysqlenv if [ ! -d $LOGSPATH ];then mkdir$LOGSPATH fi
$mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'replication'@'%' IDENTIFIED BY 'replication';flush privileges;" $mysql -e "set global innodb_support_xa=0;" $mysql -e "set global sync_binlog=0;" $mysql -e "set global innodb_flush_log_at_trx_commit=0;" $mysql -e "flush logs;" $mysql -e "reset slave all;"
################################################## #File Name : mystop.sh #Description: Set parameters to ensure that the data # is not lost, and finally check to see # if there are still write operations, # the last 1 minutes to exit
#二进制安装文件安装keepalived cd /data/softs/ wget http://www.keepalived.org/software/keepalived-2.0.7.tar.gz tar -zxf keepalived-2.0.7.tar.gz cd keepalived-2.0.7 ./configure --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --mandir=/usr/share make && make install
2.3.2 OpenStack中关于vip的配置
2.3.2.1 创建虚拟IP
2.3.2.2 虚拟机启用VIP
1 2
#在OpenStack中执行 root@control01:~# openstack port list | egrep "172.20.180.44|172.20.180.165"
#在节点1上,keepalived服务处于正常运行状态、且eth0网口上除了配置了172.20.180.44外还绑定了keepalived的vip [root@ztmonitor01 ~]# systemctl status keepalived [root@ztmonitor01 ~]# ip a | more
image-20240811155008229
1 2 3
#在节点2上,keepalived服务也处于正常运行状态,但eth0网口上只配置了172.20.180.164这个ip [root@ztmonitor02 ~]# systemctl status keepalived [root@ztmonitor02 ~]# ip a | more
image-20240811155432743
2.3.4.2
通过keepalived的vip连接MySQL
1 2 3
#通过节点1、节点2外的另外一个服务器(操作系统ip:172.24.0.31)经由keepalived的vip连接MySQL集群 root@controller01:~# ifconfig bond0.2048 root@controller01:~# mysql -h172.20.180.43 -uroot -p123456
image-20240811160425430
1 2 3 4 5 6 7
#然后在节点1上的数据库实例中,查看此数据库实例中现有的所有进程(下图将一些其他跟此验证无关的进程马赛克处理了)。其中发现存在一个来自172.24.0.31的连接进程 [root@ztmonitor01 ~]# mysql -uroot -p123456 -e "show processlist;"
#对比下,节点2上的数据库实例,除了系统自己创建的连接或主从复制创建的连接外,没有来自外部的连接 [root@ztmonitor02 ~]# mysql -uroot -p123456 -e "show processlist;"
#连接到此MySQL集群中,在db01数据库中创建一个张数据表t1 [root@ztmonitor02 ~]# mysql -h172.20.180.43 -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 467246 Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> use db02; Database changed mysql> create table t1( -> id int primary key auto_increment, -> name varchar(20) not null -> )charset utf8; Query OK, 0 rows affected (0.08 sec) mysql> exit
#可以看到在节点1上数据库实例down掉期间,在节点2上数据库实例上执行的操作被成功同步到节点1上 [root@ztmonitor01 mysql]# 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 41 Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> use db02; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed mysql> show tables; +----------------+ | Tables_in_db02 | +----------------+ | t1 | +----------------+ 1 row in set (0.00 sec)