阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux环境下MySQL5.6 Master-Slave配置实战

194次阅读
没有评论

共计 4381 个字符,预计需要花费 11 分钟才能阅读完成。

MySQL 的安装相对于 Oracle 来说非常简单,个人觉得与 TimesTen 的安装配置更为接近,而且后面的 Master-Slave 或 Master-Master 集群配置更为简单,甚至比 TimesTen 的 ASP/Active-Active/Active-Standby 配置都比 TimesTen 还要简单。
Master-Slave 搭建步骤:
1、在两台主机分别安装 MySQL 数据库软件
2、在两台主机分别创建并初始化 MySQL 数据库实例
3、从主库 dump 数据(库) 导入到备库
4、修改主从(Master-Slave) 配置
5、主库 grant、从库启动复制(start slave)
 6、验证主备数据同步

–master:
 grant replication slave,replication client on *.* to ‘slave’@’192.168.100.81’ identified by ‘slave’;

导出:
flush tables with read lock;

 mysql> insert into tony(tid,tdept) values (20001,’ty’);
 ERROR 1223 (HY000): Can’t execute the query because you have a conflicting read lock
 mysqldump -uroot -p -E -R -A –databases tyms> /mysql/data/tyms_master.sql

 slave 导入:
mysql 命令行
mysql>source /mysql/data/tyms_master.sql;

 –master:
 (root@localhost) [(none)]> show master status;
 +——————+———-+————–+——————+——————-+
 | File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
 +——————+———-+————–+——————+——————-+
 | mysql-bin.000014 |      594 |              |                  |                  |
 +——————+———-+————–+——————+——————-+
 1 row in set (0.00 sec)

 —slave:
 change master to  master_host=’192.168.100.71′, master_user=’slave’, master_password=’slave’, master_log_file=’mysql-bin.000014′, master_log_pos=594;
 (root@localhost) [(none)]> change master to  master_host=’192.168.100.71′, master_user=’slave’, master_password=’slave’, master_log_file=’mysql-bin.000014′, master_log_pos=594;
 Query OK, 0 rows affected, 2 warnings (0.02 sec)

 –master:
 unlock tables;

 —slave:
 mysql> start slave;

 [tyms]> show slave status \G
 *************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.100.71
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000014
          Read_Master_Log_Pos: 3051261
                Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 3050950
        Relay_Master_Log_File: mysql-bin.000014
              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: 3051261
              Relay_Log_Space: 3051123
              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: 1000
                  Master_UUID: 845f69fa-c558-11e5-9462-000c297b003d
              Master_Info_File: /mysql/data/mysqldata/mydata/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)

  [tyms]> show binlog events in ‘/mysql/data/mysqldata/mydata/mysql-bin.000014’ from 3051261;
 
 [(none)]> select version();
 +————+
 | version()  |
 +————+
 | 5.6.28-log |
 +————+
 1 row in set (0.00 sec)

可以看到:Slave_IO_Running | Slave_SQL_Running 两个值都是 YES,说明配置成功了。可以在主库里执行 DML 或者 DDL 验证下。
 

如果同步不成功:
1:停掉从库
mysql> slave stop
 2:主库上找到日志和位置
mysql> show master status;
 (root@localhost) [(none)]> show master status;
 +——————+———-+————–+——————+——————-+
 | File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
 +——————+———-+————–+——————+——————-+
 | mysql-bin.000014 |      594 |              |                  |                  |
 +——————+———-+————–+——————+——————-+
 3:手动同步
mysql> change master to
 > master_host=’master_ip’,
 > master_user=’gechong’,
 > master_password=’gechong’,
 > master_port=3306,
 > master_log_file=’mysql-bin.000014′,
 > master_log_pos=594;
 1 row in set (0.00 sec)
 4:启动从库
mysql> slave start;
 1 row in set (0.00 sec)

如果有异常需要可执行跳过:
>slave stop;
 >SET GLOBAL sql_slave_skip_counter = 1;
 >slave start;

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-04/130332.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计4381字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中