当前位置:   article > 正文

mysql高可用集群搭建_mysql高可用配置

mysql高可用配置
  1. 机器作用
  2. 10.10.20.147 master
  3. 10.10.20.148 slave1
  4. 10.10.20.230 slave2
  5. 10.10.20.179 mha(负载读写分离控制)

一:搭建mysql

1:环境准备,更改主机名,配置host文件

  1. hostnamectl set-hostname centos77
  2. vim /etc/host

2:配置防火墙

  1. firewall-cmd --state #查看防火墙状态
  2. service firewalld start #开启防火墙
  3. service firewalld restart #重启防火墙
  4. service firewalld stop #关闭防火墙
  5. firewall-cmd --list-all #查看防火墙规则
  6. firewall-cmd --query-port=8080/tcp # 查询端口是否开放
  7. firewall-cmd --permanent --add-port=80/tcp # 开放80端口
  8. firewall-cmd --permanent --remove-port=8080/tcp # 移除端口
  9. firewall-cmd --reload #保存防火墙

3:将相应的部署包上传到相应的目录

/home/app/

4:解压部署包

  1. cd /home/app
  2. tar -xvf mysql-8.0.26-el7-x86_64.tar
  3. tar -xzvf mysql-8.0.26-el7-x86_64.tar.gz
  4. mv mysql-8.0.26-el7-x86_64 mysql

5:创建tmp和data文件夹

mkdir {data,tmp}

6:创建用户和数组

  1. groupadd mysql
  2. useradd -r -g mysql mysql

7:给mysql目录赋权

chown -R mysql:mysql /home/app/mysql

8:初始化mysql

  1. cd /home/app/mysql/bin
  2. ./mysqld --initialize --user=mysql --datadir=/home/app/mysql/data --basedir=/home/app/mysql

9:查看初始化状态

10:配置/etc/my.cnf ###########主节点##############

  1. [client]
  2. port=3306
  3. socket=/home/app/mysql/tmp/mysql.sock
  4. [mysqld]
  5. port=3306
  6. user=mysql
  7. socket=/home/app/mysql/tmp/mysql.sock
  8. basedir=/home/app/mysql
  9. datadir=/home/app/mysql/data
  10. log-error=/var/log/mysqld.log
  11. pid-file=/var/run/mysqld/mysqld.pid
  12. log-bin=mysql-bin
  13. server-id = 1
  14. gtid_mode=ON
  15. enforce-gtid-consistency=true
  16. log_slave_updates=ON
  17. log_bin=binlog

11:配置/etc/my.cnf ###########从1节点##############

  1. [client]
  2. port=3306
  3. socket=/home/app/mysql/tmp/mysql.sock
  4. [mysqld]
  5. port=3306
  6. user=mysql
  7. socket=/home/app/mysql/tmp/mysql.sock
  8. basedir=/home/app/mysql
  9. datadir=/home/app/mysql/data
  10. log-error=/var/log/mysqld.log
  11. pid-file=/var/run/mysqld/mysqld.pid
  12. #log-bin=mysql-bin
  13. server-id = 2
  14. gtid_mode=ON
  15. enforce-gtid-consistency=true
  16. log_slave_updates=ON
  17. log_bin=binlog

12:配置/etc/my.cnf ###########从2节点##############

  1. [client]
  2. port=3306
  3. socket=/home/app/mysql/tmp/mysql.sock
  4. [mysqld]
  5. port=3306
  6. user=mysql
  7. socket=/home/app/mysql/tmp/mysql.sock
  8. basedir=/home/app/mysql
  9. datadir=/home/app/mysql/data
  10. log-error=/var/log/mysqld.log
  11. pid-file=/var/run/mysqld/mysqld.pid
  12. #log-bin=mysql-bin
  13. server-id = 3
  14. gtid_mode=ON
  15. enforce-gtid-consistency=true
  16. log_slave_updates=ON
  17. log_bin=binlog

13:创建相应的日志目录和pid目录,并赋权

  1. mkdir -p /var/log
  2. mkdir -p /var/run/mysqld
  3. chown -R mysql:mysql /var/log
  4. chown -R mysql:mysql /var/run/mysqld

14:创建mysqld服务

cp /home/app/mysql/support-files/mysql.server /etc/init.d/mysqld

15:配置环境变量

  1. vim /etc/profile
  2. PATH=/home/app/mysql/bin:/home/app/mysql/lib:$PATH
  3. export PATH
  4. source /etc/profile #生效环境变量

16:尝试启动mysql

/etc/init.d/mysqld start

17:登录mysql并更改密码和权限

  1. mysql -uroot -p
  2. #create user 'admin'@'10.10.20.%' identified with mysql_native_password by 'Mysql2021!';
  3. #grant replication slave on *.* to 'admin'@'10.10.20.%';
  4. alter user 'root'@'localhost' identified by 'Mysql2021!';
  5. alter user 'root'@'localhost' identified with mysql_native_password by 'Mysql2021!';
  6. use mysql;
  7. select host,user,plugin from user;
  8. update user set host='%' where user='root';
  9. flush privileges;

二:配置主从

1:主节点配置从节点的账号及权限

  1. create user 'repl'@'10.10.20.%' identified with mysql_native_password by 'Mysql2021!';
  2. grant replication slave on *.* to 'repl'@'10.10.20.%';
  3. install plugin rpl_semi_sync_master soname 'semisync_master.so';
  4. install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
  5. set global rpl_semi_sync_master_enabled=1;
  6. set global rpl_semi_sync_master_timeout=1000000000000;

2:从1节点配置

  1. create user 'repl'@'10.10.20.147' identified with mysql_native_password by 'Mysql2021!';
  2. grant replication slave on *.* to 'repl'@'10.10.20.%' master_auto_position=1;
  3. change master to master_host='10.10.20.147',master_user='repl',master_password='Mysql2021!',master_auto_position=1;
  4. install plugin rpl_semi_sync_master soname 'semisync_master.so';
  5. install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
  6. set global rpl_semi_sync_slave_enabled=1;
  7. stop slave io_thread;
  8. start slave io_thread;
  9. start slave;
  10. show slave status\G;

显示一下内容,说明配置成功。

3:从2节点配置

  1. change master to master_host='10.10.20.147',master_user='repl',master_password='Mysql2021!',master_auto_position=1;
  2. install plugin rpl_semi_sync_master soname 'semisync_master.so';
  3. install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
  4. set global rpl_semi_sync_slave_enabled=1;
  5. stop slave io_thread;
  6. start slave io_thread;
  7. start slave;
  8. show slave status\G;

三:在server4上安装管理节点

1:安装依赖包(四个节点都装)

  1. yum install -y epel-release perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager

2:安装mha4mysql-manager-0.58-0.el7.centos.noarch.rpm mha4mysql-node-0.58-0.el7.centos.noarch.rpm(四个节点都装)

  1. yum -y install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
  2. yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm

3.生成server4的ssh密钥,并发送给server1、server2、server3(机器名)

  1. ssh-keygen
  2. ssh-copy-id server1
  3. ssh-copy-id server2
  4. ssh-copy-id server3

4.创建masterha文件夹

  1. mkdir /etc/masterha
  2. cd /etc/masterha

5.编辑mha配置文件,vim master.cnf

  1. [server default]
  2. manager_workdir=/etc/masterha
  3. manager_log=/var/log/masterha.log
  4. master_binlog_dir=/etc/masterha
  5. #imaster_ip_failover_script= /usr/local/bin/master_ip_failover
  6. #master_ip_online_change_script= /usr/local/bin/master_ip_online_change
  7. password=**********
  8. user=root
  9. ping_interval=1
  10. remote_workdir=/tmp
  11. repl_password=Mysql2021!
  12. repl_user=repl
  13. ssh_user=root
  14. #ssh_port=22
  15. #user=admin
  16. #password=########
  17. [server1]
  18. hostname=slave0
  19. ssh_port=22
  20. port=3306
  21. #candidate_master=1
  22. #check_repl_delay=0
  23. [server2]
  24. hostname=slave1
  25. ssh_port=22
  26. port=3306
  27. candidate_master=1
  28. check_repl_delay=0
  29. candidate_master=1
  30. check_repl_delay=0
  31. [server3]
  32. hostname=slave2
  33. ssh_port=22
  34. port=3306
  35. #no_master=1

6.密钥互相传递(server1/2/3 是主机名)

  1. scp -r ~/.ssh server1:
  2. scp -r ~/.ssh server2:
  3. scp -r ~/.ssh server3:

7.检查ssh是否出错

  1. masterha_check_ssh --conf=/etc/masterha/master.cnf
  2. grant all on *.* to root@'%' identified by 'Mysql2021!';#server1执行(mysql8 执行一下操作代替:CREATE USER 'root'@'%' IDENTIFIED BY 'Mysql2021!';再给用户授权grant all privileges on *.* to 'root'@'%';)
  3. set global read_only=1;#server2执行
  4. set global read_only=1;#server3执行

8.查看mysql的复制情况

masterha_check_repl --conf=/etc/masterha/master.cnf

四:MHA测试

(1)手动测试

1.关闭server1的mysql

/etc/init.d/mysqld stop

2.手动将master节点转换到server2上

masterha_master_switch --master_state=dead --conf=/etc/masterha/master.cnf --dead_master_host=10.10.20.147 --dead_master_port=3306 --new_master_host=10.10.20.148 --new_master_port=3306

3:server2查看slave状态为空

show slave status\G;

4.server3查看slave状态(master的ip转到server2)

show slave status\G;

5.打开server1的mysql将slave添加进群组

  1. /etc/init.d/mysqld start
  2. mysql -u root -p
  3. change master to master_host='10.10.20.147',master_user='repl',master_password='Mysql2021!',master_auto_position=1;
  4. start slave;
  5. show slave status\G;

6.手动将matser转到server1上

masterha_master_switch --master_state=alive --conf=/etc/masterha/master.cnf --new_master_host=10.10.20.147 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=1000;

(2)自动转换

1.在server4下创建一个检测进程,来创建监控master的进程并查看进程,即执行自动转换命令

  1. nohup masterha_manager -conf=/etc/masterha/master.cnf &> /etc/masterha/manager.log &
  2. 查看进程
  3. ps a

2.关掉server1的mysql并查看其他节点的状态

  1. /etc/init.d/mysqld stop
  2. show slave status\G;

五:mysql配置报错

报错1:MySQL的slave_msq_running状态为no

解决办法:

首先,我们需要停止从服务器。登录mysql服务器并执行以下命令。

mysql> RESET MASTER;mysql> FLUSH TABLES WITH READ LOCK;

使用以下命令复制数据库转储。

mysqldump -uroot -pMysql2021! --set-gtid-purged=OFF --all-databases > dump.sql

在备份后解锁主服务器上的表。

mysql> UNLOCK TABLES;

登录mysql并执行以下命令以重置从服务器状态。

mysql> RESET SLAVE;

重置从服务器后启动从服务器复制

mysql> START SLAVE;

复制已与新配置重新同步,可以使用以下命令进行验证。

mysql> show slave status\G;

报错2:mysql的slave_IO_running状态为no

解决办法:

  1. 1:在从节点上执行 reset master; 重置从节点上的masterUUID
  2. 2stop slave; 停止slave会话
  3. 3reset slave; 重置slave信息
  4. 4start slave; 重启slave会话
  5. 5reset master; 重启master
  6. 6:show slave status\G; 查看状态

报错3:mha环境检查报错

原因:root全系不对

解决办法:

  1. update user set host='%' where user='root'; #更改权限
  2. flush privileges; # 刷新配置

报错4:mha环境检查报错

解决办法:

grant REPLICATION CLIENT ON *.* TO 'root';

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/976102
推荐阅读
相关标签
  

闽ICP备14008679号