Linux-centos7配置MySQL主从

47 篇文章 4 订阅
42 篇文章 0 订阅

1、首先准备两台虚拟机

2、查看MySQL的版本号,这里多说几种方式。

[root@localhost /]# mysql --help | grep Distrib
mysql  Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using  EditLine wrapper

方式二:

方式三:

  • 主库的配置

修改 my.cnf配置文件,添加如下内容

#免密登录 
#skip-grant-tables

#主从复制配置
#设置主服务 的ID (id可以自己随便设置但是要保证和slave的id不一样)
server-id=200
#设为1当然是最安全的,但性能也是最差的(相对其他两个参数而言,但不是不能接受)。如果对数据一致性和完整性要求不高,完全可以设为2,如果只追求性能,例如高并发写的日志服务器,设为0来获得更高性能
#innodb_flush_log_at_trx_commit=2
#开启binlog日志同步功能
#sync_binlog=1
#binlog日志文件名
log-bin=master-slave-bin
# 这个表示只同步某个库 (如果没有此项,表示同步所有的库)
# binlog-do-db=xxxx 

3、在主库的服务器上,给从库分配权限(主服务器新建用户并赋予“REPLICATION SLAVE”的权限)。

mysql> GRANT REPLICATION SLAVE ON *.* to 'repl'@'%' identified by 'ryTl1_12au_n0krM';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

这句 GRANT REPLICATION SLAVE ON *.* to 'repl'@'%' identified by 'ryUl1_33au_n0krQ'; 意思是所有slave都可以通过账号【repl】和密码【ryTl1_12au_n0krM】来同步master的数据。

4、查看master状态,到这里master的配置已经完成。

mysql> show master status \G;
*************************** 1. row ***************************
             File: master-slave-bin.000003
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

  • 从库的配置

5、同理修改slave服务器的mysql配置 vim /etc/my.cnf 加入下面的配置,需要注意的是server-id不要和master一样

#设置从库服务的ID (id可以自己随便设置但是要保证和slave的id不一样)
server-id=2
#binlog 日志文件名
log-bin=mysql-bin-2
#忽略的库 
replicate-ignore-db = mysql
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
slave-skip-errors=all
slave-net-timeout=60

6、进入到数据库中,执行下面的命令,这里面需要注意的就是 master_host一定要是主库服务器的ip地址。

mysql> change master to master_host='192.168.19.132',master_user='repl',master_password='ryTl1_12au_n0krM', master_log_file='master-slave-bin.000003',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

命令说明:

master_host: 主机的ip
master_user : 主机授权的用户.
master_password : 主机授权时候填写的密码
master_log_file : 主机show master status;中的File
master_log_pos: 主机show master status;中的Position

7、启动 slave

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

8、查看一下slave的状态

mysql> show slave status\G;

 

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.19.132
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-slave-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 327
        Relay_Master_Log_File: master-slave-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql,information_schema,performance_schema
           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: 538
              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: 200
                  Master_UUID: 557e9fe9-e774-11ea-8415-000c29b40512
             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)

ERROR: 
No query specified

 

9、现在就来测试一下。

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值