1、基本环境配置

1、关闭selinux和防火墙

  1. #在每一台节点上都执行
  2. systemctl stop firewalld #关闭防火墙
  3. systemctl disable firewalld
  4. sed -i -e "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
  5. setenforce 0

2、配置MySQL yum源

  1. #在每一台节点上都执行
  2. vim /etc/yum.repos.d/mysql.repo #添加MySQL yum源
  3. #添加以下内容
  4. [mysql57-community]
  5. name=MySQL 5.7 Community Server
  6. baseurl=https://mirrors.cloud.tencent.com/mysql/yum/mysql-5.7-community-el7-x86_64/
  7. enabled=1
  8. gpgcheck=0
  9. yum clean all #清除YUM缓存

2、安装

1、安装MySQL

  1. #在每一台节点上都执行
  2. yum install mysql-community-server -y

image.png

2、启动MySQL

  1. #在每一台节点上都执行
  2. systemctl start mysqld.service
  3. systemctl enable mysqld.service

image.png

3、查找MySQL初始化密码

  1. #在每一台节点上都执行
  2. grep "temporary password is generated" /var/log/mysqld.log

初始化密码
image.png

4、初始化数据库

  1. #在每一台节点上都执行
  2. mysql_secure_installation

5、进行数据库密码和安全设置:

  1. Securing the MySQL server deployment.
  2. Enter password for user root: #输入第四步获取的密码
  3. The existing password for the user account root has expired. Please set a new password.
  4. New password: #设置新密码
  5. Re-enter new password: #再输入一遍
  6. Estimated strength of the password: 100
  7. Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : yes
  8. By default, a MySQL installation has an anonymous user,
  9. allowing anyone to log into MySQL without having to have
  10. a user account created for them. This is intended only for
  11. testing, and to make the installation go a bit smoother.
  12. You should remove them before moving into a production
  13. environment.
  14. Remove anonymous users? (Press y|Y for Yes, any other key for No) : #是否删除匿名用户?按y | y表示是,按任何其他键表示否,这里我直接跳过
  15. ... skipping.
  16. Normally, root should only be allowed to connect from
  17. 'localhost'. This ensures that someone cannot guess at
  18. the root password from the network.
  19. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : #是否禁用root用户远程登录,根据自己的需求选择Y/n并回车,我这里选择允许所以直接跳过
  20. ... skipping.
  21. By default, MySQL comes with a database named 'test' that
  22. anyone can access. This is also intended only for testing,
  23. and should be removed before moving into a production
  24. environment.
  25. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : #是否删除test数据库,直接回车
  26. ... skipping.
  27. Reloading the privilege tables will ensure that all changes
  28. made so far will take effect immediately.
  29. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : #是否重新加载权限表,直接回车
  30. ... skipping.
  31. All done!

3、配置MySQL

1、主节点配置

  1. vim /etc/my.cnf
  2. 在[mysqld]添加以下内容
  3. log-bin=mysql-bin #开启二进制文件
  4. server-id=1 # 唯一值
  5. auto_increment_increment=2 #主键自动增长步长
  6. auto_increment_offset=1 #主键起始位
  7. log-slave-updates=1 #互为主从的时候两台都需要写入自己从对方取到的数据到binlog里面
  8. sync_binlog=1 #每提交一次事务同步一次二进制文件

2、备节点配置

  1. vim /etc/my.cnf
  2. 在[mysqld]添加以下内容
  3. log-bin=mysql-bin #开启二进制文件
  4. server-id=2 # 唯一值
  5. auto_increment_increment=2 #主键自动增长步长
  6. auto_increment_offset=2#主键起始位
  7. log-slave-updates=1 #互为主从的时候两台都需要写入自己从对方取到的数据到binlog里面
  8. sync_binlog=1 #每提交一次事务同步一次二进制文件

3、从节点配置

  1. vim /etc/my.cnf
  2. 在[mysqld]添加以下内容
  3. log-bin=mysql-bin #开启二进制文件
  4. server-id=3 # 唯一值

4、重启服务

  1. #在每一台节点上都执行
  2. systemctl restart mysqld

4、配置主主、主从

1、创建用户并授权

  1. 主节点和备节点执行:
  2. mysql -uroot -p
  3. Enter password: #输入你设置的密码
  4. mysql> SHOW MASTER STATUS; #看一下Master状态

image.png

  1. mysql> CREATE USER 'backup'@'%' IDENTIFIED BY 'Backup1!';
  2. GRANT REPLICATION SLAVE ON *.* TO 'backup'@'%';
  3. flush privileges;

image.png

2、主备互为主从、设置从库

  1. 主节点:
  2. #查看备机内网IP地址
  3. mysql> CHANGE MASTER TO
  4. MASTER_HOST='172.17.53.79',
  5. MASTER_USER='backup',
  6. MASTER_PASSWORD='Backup1!',
  7. MASTER_LOG_FILE='mysql-bin.000001',
  8. MASTER_LOG_POS=154;
  9. mysql> START SLAVE;
  10. mysql> SHOW SLAVE STATUS\G;
  11. 备节点:
  12. mysql> CHANGE MASTER TO
  13. MASTER_HOST='172.17.244.34',
  14. MASTER_USER='backup',
  15. MASTER_PASSWORD='Backup1!',
  16. MASTER_LOG_FILE='mysql-bin.000001',
  17. MASTER_LOG_POS=154;
  18. mysql> START SLAVE;
  19. mysql> SHOW SLAVE STATUS\G;
  20. 从节点:
  21. mysql> CHANGE MASTER TO
  22. MASTER_HOST='172.17.244.34',
  23. MASTER_USER='backup',
  24. MASTER_PASSWORD='Backup1!',
  25. MASTER_LOG_FILE='mysql-bin.000001',
  26. MASTER_LOG_POS=154;
  27. mysql> START SLAVE;
  28. mysql> SHOW SLAVE STATUS\G;

image.png
自行测试下主库上面写数据备库和从库能否同步,然后再测试备库写数据主库和从库能否同步,我做了很多次测试都是可以的。