构建ha需要两台服务器,例如node1、node2
1. 下载rpm包:
打开网址https://downloads.mysql.com/archives/community/,根据操作系统类型和版本,选择安装包,此处以centos 7为例
2. 需要下载以下几个包,小版本可以根据需要选择:
mysql-community-client-5.7.32-1.el7.x86_64.rpm
mysql-community-common-5.7.32-1.el7.x86_64.rpm
mysql-community-devel-5.7.32-1.el7.x86_64.rpm
mysql-community-libs-5.7.32-1.el7.x86_64.rpm
mysql-community-server-5.7.32-1.el7.x86_64.rpm
3. 执行yum install *.rpm -y进行安装
4. 修改配置文件,
打开/etc/my.cnf
将node1的该配置文件配置为:
[mysql]
no-auto-rehash
[mysqld]
symbolic-links = 0
datadir=/var/lib/mysql/
skip-host-cache
skip-name-resolve
server_id=1
log-bin=mysql-bin
relay-log=relay-bin
relay-log-index=relay-bin.index
auto_increment_increment=2
auto_increment_offset=1
log-error = /var/log/mysqld.log
将node2的该配置文件配置为:
[mysql]
no-auto-rehash
[mysqld]
symbolic-links = 0
datadir=/var/lib/mysql/
skip-host-cache
skip-name-resolve
server_id=2
log-bin=mysql-bin
relay-log=relay-bin
relay-log-index=relay-bin.index
auto_increment_increment=2
auto_increment_offset=2
log-error = /var/log/mysqld.log
5. 启动mysql
分别执行service mysqld restart
6. 修改初始密码,两台服务器都要操作
执行如下命令,cat /var/log/mysqld.log |grep generated
会提示:
[Note] A temporary password is generated for root@localhost: BrC4uoy&6dC/
BrC4uoy&6dC/即为初始密码
通过mysql -uroot -p回车,按提示输入密码即可,进入mysql命令行后,执行:
set global validate_password_policy=0;
alter user 'root'@'localhost' identified by 'testpwd111';
GRANT RELOAD, SUPER, REPLICATION SLAVE ON *.* TO 'root'@'%' identified by 'testpwd111'
flush privileges;
7. 建立HA
两台服务器的密码都修改之后,登录进入node1的mysql提示符,执行如下操作:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 706 | | | |
+------------------+----------+--------------+------------------+-------------------+
注意其中的file和position,需要填到后续命令的参数master_log_file和master_log_pos中
在node2,执行:
change master to master_host=node1, master_user='root', master_password='testpwd123', master_port=3306, master_log_file='mysql-bin.000002', master_log_pos=706;
start slave;
在mysql提示符下执行:
show slave status\G
如果返回内容中有:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
说明HA建立成功。
其他ha的配置操作可以参考:https://blog.csdn.net/LengKuDeWenRou/article/details/110649421