MySQL主从复制案例

1.关闭防火墙和selinux
2.安装必须的软件环境

[root@mysql ~] yum -y install wget vim net-tools
[root@mysql ~] yum -y groupinstall "Development Tools"
[root@mysql ~] wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
[root@mysql ~] rpm -ivh mysql80-community-release-el7-1.noarch.rpm

⚠️需要修改MySQL的安装仓库的设置, 开启5.7的安装源, 关闭8.0的安装源
3.安装配置过程

[root@mysql ~] yum -y install mysql-community-server
[root@mysql ~] systemctl enable mysqld && systemctl start mysqld

####################################################################################

master-slave 配置过程

一、主库配置
1.vim /etc/my.cnf

[mysqld]
log_bin=mysql_bin
binlog-format=Row
server-id=1

2.初始化(针对新装的MySQL)

[root@master ~]# systemctl start mysqld && systemctl enable mysqld
[root@master ~]# grep 'password' /var/log/mysqld.log
2018-09-28T01:53:04.173732Z 1 [Note] A temporary password is generated for root@localhost: ied,1qj(y/gH //注意修改密码

[root@master ~]# mysql -uroot -p'(BavDu..0928)'

3.为从库授权用户和密码

mysql> create user 'dalong'@'10.0.123.122' identified by "123";
Query OK, 0 rows affected (0.01 sec)

mysql> grant REPLICATION SLAVE ON *.* TO 'dalong'@'10.0.123.122';
Query OK, 0 rows affected (0.00 sec)

mysql>FLUSH PRIVILEGES;

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
(注意这个154,一会将会通过这个数字作为bin_log标识符,来时从库连接日志)


二、从库配置
1.vim/etc/my.cnf
[mysqld]
server_id=22

2.初始化(针对新装的MySQL)

[root@slave ~] systemctl start mysqld && systemctl enable mysqld
[root@slave ~] grep 'password' /var/log/mysqld.log
2018-09-28T02:07:33.890742Z 1 [Note] A temporary password is generated for root@localhost: dFS:z5DQ/

3.配置从库

mysql> change master to
    -> master_host='10.0.123.124',
    -> master_user='dalong',
    -> master_password='123',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.123.124
                  Master_User: dalong
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: localhost-relay-bin.000003
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

⚠️必须出现上面两个yes 才为AB复制搭建成功

问题: 主从数据不同步怎么解决?

什么原因造成主从不同步? (硬件差距过大)

你可能感兴趣的:(MySQL主从复制案例)