【MySQL5.7主从复制】

MySQL主从复制

一 原理

*基于语句的复制。在主服务器上执行的SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。
*基于行的复制。把改变的内容复制过去,而不是把命令在从服务器上执行一遍。

二 环境

1,准备两台CentOS 7 都安装上MySQL数据库
master 192.168.226.135
slave 192.168.226.138
2,保证两台服务器的时间一致
安装时间同步服务器(ntp服务)

三步骤

1,在主服务器上安装时间同步服务器(ntp服务),使用从服务器去和主服务器的时间进行同步。

  1. #主服务器配置为时间同步服务器
    yum -y install ntp #使用yum源安装ntp服务
  2. vim /etc/ntp. conf #修改ntp服务配置文件
    将第二行未注释的“restrict default nomodify notrap noquery”修改成:restrict default nomodify #允许任何IP 的客户机都可以进行时间同步
  3. systemctl restart ntpd #重启生效
  4. systemctl enable ntpd #设置开机自启动
  5. #从服务器配置为时间同步客户端
  6. yum -y install ntpdate #安装ntpdate
  7. ntpdate 192. 168.226.135 #和主服务器的时间进行同步

2 安装myaql数据库5.7

  1. #配置MySQL5.7源
    wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm 下载
    rmp -ivh mysql80-community-release-el7-7.noarch.rpm 安装

  2. #安装MySQL
    yum -y install mysql-community- server

  3. systemctl restart mysqld #重启一下数据库保证能够登录到MySQL

  4. grep “password” /var/log/mysqld.log #查询初始随机密码

  5. mysql -u root -p #使用初始密码登录

  6. password:

  7. #修改源文件
    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 #启用5.7版本MySQL
gpgcheck=0 #不检查错误
gpgkey=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 #不启用8.0版本MySQL
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  1. yum makecache #重新生成缓存
  2. mysql> set global validate_ password_ policy=0; #密码策略全局变量改掉
  3. mysql> alter user user() identified by ‘xxx’; #设置新密码,默认情况下user表中的user字段只有root一个用户
  4. mysql>select version(); #查看数据库的版本
  5. mysql>grant all on . to ‘root’@‘%’ identified by ‘xxx’; #创建允许远程登陆
  6. mysql>flush privileges; #刷新策略

3 主服务器配置

  1. vim /etc/my.cnf #修改配置文件 在文末增加下面内容
server-id = 11
log_ _bin = master- bin
log-slave-updates = true
  1. mysq|> grant replication slave on . to ‘myslave @’ 192.168.226. %’ identified by ‘xxx’; #为从服务器添加授权
  2. mysql> flush privileges; #刷新用户授权
    Query OK, 0 rows affected (0.01 sec)
  3. mysq|> show master status; #查看master状态,找到position字段信息,这里是154
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000003 |      154 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

#File 主服务器的二进制日志文件名
#Position 主服务器的修改记录

4、配置slave从服务器

  1. vim /etc/my .cnf #在文末增加下面内容
server-id = 22
relay-log = relay-log-bin
relay-log-index = slave-relay-bin. index
  1. systemctl restart mysqld #登录从服务器的MySQL上,配置同步
    mysq|> change master to master_ host=’ 192.168.226.135’ ,master_ user=’ myslave ,master_ password= ‘xxx’ ,master_ log_ file=‘master-bin. 000003’ ,master_ log_ pos= 154;
    #这里的master_ log_ pos=154 为master服务器上position字段信息
  2. mysq|> start slave; #开启服务
  3. mysq|> show slave status\G; 查看是否成功
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.226.135
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 324
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes  **#为yes成功**
            Slave_SQL_Running: Yes **#为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: 154
              Relay_Log_Space: 532
              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: 11
                  Master_UUID: c25c6b7b-3fc4-11ed-8f87-000c296435cb
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica 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: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.01 sec)

ERROR: 
No query specified
  1. 在master上创建数据库,然后在slave上就可以查看到对应的数据库
    在master上创建wangwu数据库
mysql> create database wangwu;
Query OK, 1 row affected (0.05 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| 1                  |
| mysql              |
| performance_schema |
| student            |
| sys                |
| wangwu             |
| yy                 |
+--------------------+
8 rows in set (0.00 sec)

在slave上查看就有wangwu数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wangwu             |
+--------------------+
5 rows in set (0.03 sec)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值