# MySQL高可用架构集群环境搭建手册
## 环境准备
### 机器规划
| 节点 | IP | 配置 | 角色 |
| -------- | -------------- | ---- | ---------------- |
| master-1 | 192.168.73.129 | 1c2g | master、HMA node |
| slave-1 | 192.168.73.130 | 1c1g | salve、HMA node |
| slave-2 | 192.168.73.131 | 1c1g | salve、HMA node |
| slave-3 | 192.168.73.132 | 1c1g | HMA manager |
### 安装包
mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
拷贝到master和salve节点的/opt/software/mysql/目录下并解压
```shell
tar -xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
```
### 安装Mysql
在192.168.73.129、192.168.73.130、192.168.73.131分别按如下步骤命令安装Mysql。
1)卸载自带的mariadb
```shell
rpm -qa|grep mariadb
rpm -e mariadb-libs-5.5.64-1.el7.x86_64 --nodeps
```
2)安装Mysql
```shell
rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.28-1.el7.x86_64.rpm
```
3)初始化mysql
```shell
mysqld --initialize --user=mysql
```
4)查看临时密码
```shell
[root@localhost mysql]# cat /var/log/mysqld.log
2020-07-27T11:35:17.156086Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-07-27T11:35:17.291065Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-07-27T11:35:17.321303Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-07-27T11:35:17.389391Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 3eb43224-cffd-11ea-b6e3-000c2998582e.
2020-07-27T11:35:17.390659Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-07-27T11:35:17.797306Z 0 [Warning] CA certificate ca.pem is self signed.
2020-07-27T11:35:17.916216Z 1 [Note] A temporary password is generated for root@localhost: =L=9m4yg29kA
```
5)启动mysql,并设置开机启动
```shell
[root@localhost mysql]# systemctl start mysqld.service
[root@localhost mysql]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2020-07-27 19:37:41 CST; 5s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 2328 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 2311 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 2331 (mysqld)
CGroup: /system.slice/mysqld.service
└─2331 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
7月 27 19:37:41 localhost.localdomain systemd[1]: Starting MySQL Server...
7月 27 19:37:41 localhost.localdomain systemd[1]: Started MySQL Server.
```
6)登录mysql,修改密码
```shell
[root@localhost mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28
Copyright (c) 2000, 2019, 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> set password=password('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
```
7)关闭防火墙
```shell
[root@localhost mysql]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 日 2020-07-26 23:36:30 CST; 20h ago
Docs