MySQL主从复制

MySQL主从复制

1.环境准备

  • 准备两台机器

  • 192.168.222.137

    192.168.222.138

  • 操作系统为CentOS7

  • 修改主机名与主机名解析

    #master
    [root@localhost ~]# hostnamectl set-hostname master
    [root@localhost ~]# bash
    #slave
    [root@localhost ~]# hostnamectl set-hostname slave
    [root@localhost ~]# bash
    #主机名解析(两台机器均操作)
    [root@master ~]# vim /etc/hosts
    192.168.222.137 slave
    192.168.222.138 master
    

2.安装MySQL

#安装版本为5.7(两台机器均安装)
#安装MySQL5.7的源
[root@master ~]# yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
#修改MySQL源
[root@master ~]# vim /etc/yum.repos.d/mysql-community.repo
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2023
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2023
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[root@master ~]# yum -y install mysql-community-server

3.修改配置文件

1.master

#启动主从上的MySQL
[root@master ~]# systemctl start mysqld
#更改密码
[root@master ~]# mysqladmin -uroot -p"`awk '/A temporary password/{p=$NF}END{print p}' /var/log/mysqld.log`" password "Yhb20014319."
#开启binlog
[root@master ~]# mkdir -pv /data
[root@master ~]# chown -R mysql.mysql /data
#在[mysqld]下添加
[root@master ~]# vim /etc/my.cnf
[mysqld]
server-id=201
log-bin=/var/log/mysql/mysql-bin
#重启MySQL
[root@master ~]# systemctl restart mysqld
#授权主从复制账号
[root@master ~]# mysql -uroot -pYhb20014319.
mysql> GRANT REPLICATION SLAVE ON *.* TO 'takehaye'@'%' IDENTIFIED BY BY 'Yhb20014319.'
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1374 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

2.配置从库

#开启binlog
[root@slave ~]# mkdir -pv /data
[root@slave ~]# chown -R mysql.mysql /data
#在[mysqld]下添加
[root@slave ~]# vim /etc/my.cnf
[mysqld]
server-id=202				#两台机器server-id不能一样
log-bin=/data/mysql-bin
[root@slave ~]# systemctl restart mysqld
[root@slave ~]# mysql -uroot pYhb20014319.
mysql> change master to
    -> master_host='master',
    -> master_user='takehaye',
    -> master_password='Yhb20014319.',
    -> master_port=3306,
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=1374;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master
                  Master_User: takehaye
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1374
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1374
              Relay_Log_Space: 527
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 201
                  Master_UUID: 95045d6b-3069-11ef-9ee0-000c297a6b17
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

注:13、14行YES表示主从复制正常

21、22行表示错误码与错误信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值