主从经典复制AB

12 篇文章 0 订阅
3 篇文章 0 订阅

在这里插入图片描述
画好架构图

1、 克隆一台 虚拟机 ( 按照做好初始化,模板机,初始化脚本 )。

  配置好IP地址
  配置好主机名
  去掉 /etc/rc.d/rc.local  主机名 设置段

2、 补充 boost

[root@mysql-80 /usr/local]# scp -r /usr/local/boost   192.168.10.81:/usr/local/  

3、 补充其它相关 软件包

 [root@mysql-81 /usr/local]# yum  -y   install   gcc-c++   ncurses-devel   cmake

4、添加mysql 用户 和用户 组

[root@mysql-81 /usr/local]# groupadd -g 27 mysql
[root@mysql-81 /usr/local]# useradd -u 27 -g mysql  -M   -s  /sbin/nologin  mysql

5、 移植 mysql57 程序

[root@mysql-80 /usr/local]# scp -r /usr/local/mysql57   192.168.10.81:/usr/local

6、移植 并修改 配置文件

[root@mysql-80 /usr/local]# scp /etc/my.cnf   192.168.10.81:/etc/
从服务器 192.168.10.81 的配置文件

[client]
port=3306
socket=/tmp/mysql.sock
password=123123

[mysqld]
default_password_lifetime=30
character-set-server=utf8
collation-server=utf8_general_ci
default_storage_engine=InnoDB
max_connections=2000
#max_connect_errors=100

innodb_buffer_pool_size=800M
innodb_flush_log_at_trx_commit=2
innodb_file_per_table=ON
innodb_data_home_dir="/data/mysql"
innodb_data_file_path=ibdata1:12M:autoextend
innodb_log_buffer_size=16M
innodb_log_file_size=52428800
innodb_log_files_in_group=2
#innodb_log_group_home_dir="./"
transaction-isolation=REPEATABLE-READ

general_log=OFF
log_output=file
general_log_file=/data/mysql/general.log
slow_query_log=ON
slow_query_log_file=/data/mysql/slow.log
long_query_time=2.000000
#log-bin=/bin-log/mysql-bin
server-id=81
#max_binlog_size=1073741824
#expire_logs_days=0
#binlog_format=row
#binlog_rows_query_log_events=OFF

relay-log=/bin-log/relay-log
relay-log-index=/bin-log/relay-log.index

skip-name-resolve
#skip-grant-tables
user=mysql
port=3306
basedir=/usr/local/mysql57
datadir=/data/mysql
tmpdir=/tmp
socket=/tmp/mysql.sock

query_cache_size=128M
query_cache_type=OFF
query_cache_wlock_invalidate=OFF

log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

7、根据配置文件,补充相关的目录 ,并做好授权

[root@mysql-81 /usr/local]# mkdir -p /data/mysql
[root@mysql-81 /usr/local]# mkdir -p /bin-log
[root@mysql-81 /usr/local]# chown mysql:mysql /data/mysql
[root@mysql-81 /usr/local]# chown mysql:mysql /bin-log

8、给主服务器,添加一个统一授权账户,用于 复制, 并给 主服务器 做一次 完全备份 (使用 lvm 的完全备份脚本)

[root@mysql-21 /backup]#mysql
mysql> grant all on . to ‘uplooking’@‘192.168.10.%’ identified by “123123”;
mysql> flush privileges;

[root@mysql-80 /backup]# /test/full-backup.sh
对 /data/mysql 的完全备份开始 …
锁定表…
解锁表…
快照已经成功移除
备份完成 用时 4 秒,锁表时间 1 秒
[root@mysql-80 /backup]# ll
总用量 1448
-rw------- 1 root root 1192 4月 17 19:07 my-2019-04-17-19-07-04.cnf
-rw-r–r-- 1 root root 1475601 4月 17 19:07 mysql-2019-04-17-19-07-04.tar.gz

注意,在从服务器 启动前, 此时主服务器不能 再有数据更新,直到从服务 start 为止
锁表、防火墙、停止服务 手段

9、将 完全备份的数据, 拷贝至 从服务器 ;

[root@mysql-21 /backup]# scp mysql-2019-04-17-19-07-04.tar.gz 192.168.217.25:/data/mysql/

10、 解压 备份数据,并调整数据目录

[root@mysql-81 /data/mysql]# tar xf mysql-2019-04-17-15-16-20.tar.gz
[root@mysql-81 /data/mysql]# mv tmp-mysql/* /data/mysql
[root@mysql-81 /data/mysql]# rm -rf tmp-mysql mysql-2019-04-17-15-16-20.tar.gz

****检查 数据 文件的 权限 *******

11、修改 从 服务器的 auto.cnf 中的 UUID 值

[root@mysql-81 /data/mysql]# vim /data/mysql/auto.cnf ##定长的,只能修改,不能删减 不能增加
[auto]
server-uuid=79f803d9-6637-11e9-a096-000c29e463d8 #修改最后一位,跟主 不相同即可

12、 启动从 服务器

[root@mysql-81 /bin-log]# /usr/local/mysql57/support-files/mysql.server start
[root@mysql-81 /bin-log]#netstat -ntl

13、 测试登录
[root@mysql-81 /bin-log]# cp /usr/local/mysql57/bin/mysql /usr/local/sbin/
[root@mysql-81 /bin-log]# mysql

    ####  此时 ,主从 服务器的 数据 应该 是一样 的  #### 

============= 开始 给 80 81 之间 创建 主从之间的 复制 关系 ========================

1、主服务器查看状态

mysql> show master status;
±-----------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±-----------------±---------±-------------±-----------------±------------------+
| mysql-bin.000050 | 4831 | | | |
±-----------------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)

2、从 服务器查看状态 , 状态为空

mysql> show slave status;
Empty set (0.00 sec)

3、给从服务器 设置 主服务器的信息,和同步 日志,同步位置

mysql> change master to master_host=‘192.168.10.80’,master_user=‘uplooking’,master_password=‘123123’,master_log_file=‘mysql-bin.000050’,master_log_pos=4381;

该 主服务器的 信息 会被记录, 如果需要清除该信息,使用 reset slave; 语句

4、启动 从服务器 start slave; stop slave;

mysql> start slave;

5、 启动成功, 创建 中继日志。 并可以查看从 服务器状态 。

mysql> show slave status \G

*************************** 1. row ***************************

           Slave_IO_State: Waiting for master to send event
              Master_Host: 192.168.10.80
              Master_User: uplooking
              Master_Port: 3306
            Connect_Retry: 60
          Master_Log_File: mysql-bin.000052
      Read_Master_Log_Pos: 6106
           Relay_Log_File: relay-log.000004
            Relay_Log_Pos: 4997
    Relay_Master_Log_File: mysql-bin.000052
         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: 6106
Relay_Log_Space: 5198
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: 80
Master_UUID: 79f803d9-6637-11e9-a096-000c29e463d2
Master_Info_File: /data/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)

6、 上述 情况正常后, 主服务器 才能 正常 的 更新数据。 主从同步完成!

测试1 : 主服务器 插入数据, 从服务器是同步更新 ?

测试2: 从服务器停止服务, 在此期间内,主服务 更新 20 条数据, 从 服务器 启动服务
从 是否能够自动 追 上 主的数据 ??

测试3: 尝试 向 从服务器 写 入 一条数据, 然后 ,再让 主服务器 写入一条数据,
从服务器的 SQL 线程状态,会如何?? 我们又如何修复???

         (非法尝试,从服务应该 只读 状态,禁止在 从修改数据。)

Last_SQL_Errno: 1062
Last_SQL_Error: Could not execute Write_rows event on table class.students; Duplicate entry ‘2531’ for key ‘PRIMARY’, Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event’s master log mysql-bin.000052, end_log_pos 6455

解决方法:

移除从服务器发生 冲突的主键记录。
mysql> delete from class.students where sid=2531;
Query OK, 1 row affected (0.00 sec)

重启从服务器
mysql> stop slave;
mysql> start slave;

            为了避免上述情况发生:
             给 从服务器 加上  只读  属性。 (超级只读 root 都改不了 ,  普通只读,只针对 非 root 账户 例如 uplooking )
            
            read_only=ON    ## 推荐  写入配置文件,重启生效 
            super_read_only=ON
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值