Linux Mysql主从复制

4 篇文章 0 订阅
怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下:
1、主从服务器分别作以下操作:
  1.1、版本一致
  1.2、初始化表,并在后台启动mysql
  1.3、修改root的密码

2、如果是用虚拟机克隆的mysql需要走这一步
因为是克隆所以server-uuid一样,需要改成不一样,文件在locate auto.cnf

2、必须放在最后一行
修改主服务器master:(10.101.110.45)
   #vi /etc/my.cnf
       [mysqld]
       log-bin=mysql-bin   #[必须]启用二进制日志
       server-id=1     #[必须]服务器唯一ID,默认是1,
       binlog-do-db=ourneeddb  #需要同步的数据库

       binlog-ignore-db=mysql  #不同步mysql系统数据库,若还有其它不想同步的,继续添加

3、修改从服务器slave:(10.101.110.58)
   #vi /etc/my.cnf
       [mysqld]
       server-id=2  #设置从服务器id,必须于主服务器不同
       relay-log=mysql-relay #打开relaylog
       binlog-do-db=ourneeddb  #需要同步的数据库
       binlog-ignore-db=mysql  #不同步mysql系统数据库,若还有其它不想同步的,继续添加

4、重启两台服务器的 service mysql restart
  

5、在主服务器上建立帐户并授权slave:(用来从服务器连接主服务器)

   #/usr/local/mysql/bin/mysql -uroot -proot   

   mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* 
    -> TO mysql_backup@'%' 

    -> IDENTIFIED BY '123456';

在主服务器建立好用户,最好在从服务器mysql -h主的ip -u用户名 -p是否能访问主的mysql数据库,如果不能可能是防火墙的问题
6、登录主服务器的mysql,查询master的状态
   mysql>show master status;
   +------------------+----------+--------------+------------------+
   | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
   +------------------+----------+--------------+------------------+
   | mysql-bin.000004 |      308 |              |                  |
   +------------------+----------+--------------+------------------+
   1 row in set (0.00 sec)
   注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化

7、配置从服务器Slave:

 mysql> stop slave;

 mysql>  CHANGE MASTER TO master_host = '127.0.0.1',
    ->  master_user = 'mysql_backup',
    ->  master_password = '123456',
    ->  master_log_file = 'mysql-bin.000008',

    ->  master_log_pos = 120;

   Mysql>start slave;    //启动从服务器复制功能

8、检查从服务器复制功能状态:

   mysql> show slave status\G

   *************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
              Master_Host: 192.168.2.222  //主服务器地址
              Master_User: mysync   //授权帐户名,尽量避免使用root
              Master_Port: 3306    //数据库端口,部分版本没有此行
              Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
              Read_Master_Log_Pos: 600     //#同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos
              Relay_Log_File: ddte-relay-bin.000003
              Relay_Log_Pos: 251
              Relay_Master_Log_File: mysql-bin.000004
              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: 420
              Relay_Log_Space: 452
              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: 1
                  Master_UUID: 24529cd4-b96f-11e7-9160-000c29807e7e
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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

1 row in set (0.00 sec)

注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
以上操作过程,主从服务器配置完成
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值