安装mysql
查看之前文章
Centos7,redhat安装Mysql - 简书 (jianshu.com)
配置 my.cnf
配置mysql1:
[mysqld]
basedir=/data/mysql
datadir=/data/mysql/data
socket=/data/mysql/mysql.sock
character_set_server = utf8
lower_case_table_names=1
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin=/data/mysql/mysql-bin-master
server-id=1
binlog-ignore-db=performance_schema
binlog-ignore-db=information_schema
binlog-ignore-db=sys
binlog-ignore-db=mysql
slave-skip-errors = all
relay_log=mysql-relay-bin
log-slave-updates=1
sync_binlog = 1
auto_increment_offset = 1
auto_increment_increment = 2
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_allowed_packet = 512M
innodb_log_file_size = 2GB
default-storage-engine = INNODB
max_connections=3000
max_user_connections=800
innodb_lock_wait_timeout = 50
wait_timeout = 86400
interactive_timeout = 86400
max_connect_errors = 10000
back_log=500
transaction_isolation = READ-COMMITTED
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
default-time_zone = '+8:00'
[client]
port=3306
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
socket=/data/mysql/mysql.sock
配置mysql2:
如下配置不同,其它与mysql1一样
server-id=3
auto_increment_offset = 2
查看mysql1、mysql2 的master状态
- 重启mysql1、mysql2
systemctl restart mysqld
- 分别查看mysql1、mysql2的状态
show master status;
mysql1 如下:
mysql2 如下:
上图mysql1与mysql2的File和Position这两个参数在后续步骤用于建立关系
分别登录mysql1、mysql2进行授权
这里以root用户为列
grant replication slave on *.* to 'root'@'%' identified by '当前mysql root密码';
grant all privileges on *.* to 'root'@'%' identified by '当前mysql root密码';
flush privileges;
开通mysql1、mysql2的3306端口
开通方式参考
centos firewall相关操作 - 简书 (jianshu.com)
mysql1连接mysql2
mysql -uroot -p mysql1密码 -h mysql2IP -P 3306
mysql2连接mysql1
mysql -uroot -p mysql1密码 -h mysql1IP -P 3306
配置 mysql1、 mysql2 相互同步
- 建立mysql1 与 mysql2的关系
change master to master_host='mysql2的IP',master_port=3306,master_user='root',master_password='mysql2 root用户密码',master_log_file='上图mysql2的File(mysql-bin-master.000013)',master_log_pos=上图mysql2的Podition(1746);
mysql1启动slave,并查看状态
start slave;
show slave status \G;
Slave_IO_Running和Slave_SQL_Running都为YES,则配置成功。如果不是则配置失败,需要查看到mysql2的3306是否开通。
- 建立mysql2 与 mysql1的关系
与上面一样,只是效mysql2相关换成mysql1
change master to master_host='mysql2的IP',master_port=3306,master_user='root',master_password='mysql1 root用户密码',master_log_file='上图mysql1的File',master_log_pos=上图mysql1的Podition;
测试
这里省略