第17章MySQL主从配置

青春壹個敷衍的年華 提交于 2021-02-01 11:19:58

mysql安装总结

  • mysql主从准备工作:
  • 准备两台机器,每台机器安装msyql服务,并启动mysql服务
  • mysql详细安装

1.首先下载二进制免编译的包,下载到/usr/local/src/目录下

2.解压压缩包

3.解压完之后,把解压出来的目录放到 /usr/local/mysql/ 目录下

  • 注意点:
  • 首先检查 /usr/local/mysql/ 目录是否存在
  • 若是这个目录存在,首先把这个目录改个名字,或者把目录下的内容删除
  • 然后把解压出来的目录放到 /usr/local/mysql/ 目录下面
  • 目录内容应该如下
[root@xuexi-001 ~]# ls /usr/local/mysql
bin      data  include  man     my-new.cnf  README   share      support-files
COPYING  docs  lib      my.cnf  mysql-test  scripts  sql-bench

4.然后切换到 /usr/local/mysql/ 目录下,进行初始化 命令

  • 初始化命令 ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  • 注意点:
  • 其中的--user=mysql 需要提前创建

5.初始化成功的标志就是两个OK,或者用 echo $? 检查是否初始化成功

6.编辑 /etc/my.cnf 文件——>默认是自带 my.cnf 文件的

在 /etc/my.cnf 文件中
定义 datadir=/data/mysql     
定义 socket=/tmp/mysql.sock

7.拷贝启动脚本

  • 命令 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

8.编辑启动脚本

vim /etc/init.d/mysqld //对以下两行进行指定路径

指定basedir的路径 /usr/local/mysql  
指定datadir的路径 /data/mysql

9.之后就可以启动mysql了

  • 命令 /etc/init.d/mysql start

10.如果启动失败,可以去查看错误日志

11.建议 :

  • 查看 /data/mysql 目录下的文件,默认属主、属组,如果不是mysql的,启动时会因无法写入数据而不能启动mysql
  • 改变属主和属组,命令 chomd mysql:mysql /data/mysql
  • 然后就可以尝试启动,命令 /etc/init.d/mysql start

12.若想开机启动,只需要输入命令

  • 命令 chkconfig mysqld on

配置主

  • 主从配置 - 主上操作
  • 安装mysql
  • 修改my.cnf,增加server-id=130和log_bin=aminglinux1
  • 修改完配置文件后,启动或者重启mysqld服务
  • 把mysql库备份并恢复成aming库,作为测试数据
  • mysqldump -uroot mysql > /tmp/mysql.sql
  • mysql -uroot -e “create database aming”
  • mysql -uroot aming < /tmp/mysql.sql
  • 创建用作同步数据的用户
  • grant replication slave on . to 'repl'@slave_ip identified by 'password';
  • flush tables with read lock;
  • show master status;

主从配置 - 主上操作

1.在两台机器安装并启动mysql服务后,首先在主上进行操作

2.修改/etc/my.cnf配置文件

  • 在配置文件下[mysqld]下添加
  • server_id=130 这个id可以自定义,这里根据ip来定义
  • log_bin=xuexi1打开binlog,名字自定义为log_bin=xuexi1 最终如下
[root@xuexi-001 ~]# vim /etc/my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]
server_id = 130
log_bin=xuexi1
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

保存退出

3.更改完配置文件后,需要重启mysql /etc/init.d/mysqld restart

[root@xuexi-001 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL.. SUCCESS! 

4.这时候来/data/mysql/目录下,会生成一些文件

  • ls -lt /data/mysql/
[root@xuexi-001 ~]# cd /data/mysql/
[root@xuexi-001 mysql]# ls -lt
总用量 110944
-rw-rw---- 1 mysql mysql 50331648 8月  31 22:57 ib_logfile0
-rw-rw---- 1 mysql mysql 12582912 8月  31 22:57 ibdata1
-rw-rw---- 1 mysql mysql   276358 8月  31 22:57 xuexi-001.err
-rw-rw---- 1 mysql mysql        5 8月  31 22:57 xuexi-001.pid
-rw-rw---- 1 mysql mysql       32 8月  31 22:57 xuexi1.index
-rw-rw---- 1 mysql mysql      120 8月  31 22:57 xuexi1.000002
-rw-rw---- 1 mysql mysql      143 8月  31 22:57 xuexi1.000001
drwx------ 2 mysql mysql      324 8月  30 07:56 zrlog
drwx------ 2 mysql mysql     4096 7月  25 22:21 mysql2
drwx------ 2 mysql mysql       48 7月  23 00:12 db1
-rw-rw---- 1 mysql mysql       56 7月   3 23:41 auto.cnf
drwx------ 2 mysql mysql     4096 7月   3 23:33 mysql
drwx------ 2 mysql mysql     4096 7月   3 23:33 performance_schema
-rw-rw---- 1 mysql mysql 50331648 7月   3 23:33 ib_logfile1
drwx------ 2 mysql mysql        6 7月   3 23:33 test

  • 其中 .index 索引页,这个文件是必须要有的
  • 其中 .000001

5.这个是二进制日志文件,会持续生成2、3、4等等(这个文件是实现主从配置的根本,没有这个文件根本没有办法完成主从)

6.测试,准备一个数据做演示用的 首先做一个备份

  • mysqldump -uroot -p123456 zrlog > /tmp/zrlog.sql
[root@xuexi-001 ~]# mysqldump -uroot -phanfeng zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.

7.然后创建一个新的库

  • mysql -uroot -p123456 -e "create database xuexi"
[root@xuexi-001 mysql]# mysql -uroot -p123456 -e "create database xuexi"
Warning: Using a password on the command line interface can be insecure.

8.创建好库后,还需要把数据恢复一下,那也就是说做的主从,参考的对象就是 xuexi 这个库

  • mysql -uroot -p123456 xuexi < /tmp/zrlog.sql
[root@xuexi-001 mysql]# mysql -uroot -p123456 xuexi < /tmp/zrlog.sql 
Warning: Using a password on the command line interface can be insecure.

9.再来查看/data/mysql/目录下的文件 ls -lt /data/mysql/

[root@xuexi-001 mysql]# ls -lt /data/mysql/
总用量 225592
-rw-rw---- 1 mysql mysql 50331648 8月  31 23:16 ib_logfile0
-rw-rw---- 1 mysql mysql 79691776 8月  31 23:16 ibdata1
-rw-rw---- 1 mysql mysql    10348 8月  31 23:16 xuexi1.000002
drwx------ 2 mysql mysql      324 8月  31 23:16 xuexi
-rw-rw---- 1 mysql mysql   276358 8月  31 22:57 xuexi-001.err
-rw-rw---- 1 mysql mysql        5 8月  31 22:57 xuexi-001.pid
-rw-rw---- 1 mysql mysql       32 8月  31 22:57 xuexi1.index
-rw-rw---- 1 mysql mysql      143 8月  31 22:57 xuexi1.000001
drwx------ 2 mysql mysql      324 8月  30 07:56 zrlog
drwx------ 2 mysql mysql     4096 7月  25 22:21 mysql2
drwx------ 2 mysql mysql       48 7月  23 00:12 db1
-rw-rw---- 1 mysql mysql       56 7月   3 23:41 auto.cnf
drwx------ 2 mysql mysql     4096 7月   3 23:33 mysql
drwx------ 2 mysql mysql     4096 7月   3 23:33 performance_schema
-rw-rw---- 1 mysql mysql 50331648 7月   3 23:33 ib_logfile1
drwx------ 2 mysql mysql        6 7月   3 23:33 test

10.能看到xuexi1.000002二进制文件是有增加的,xuexi1.000002增长的大小是和zrlog这个库的保持一致的,xuexi1.000002文件里完整的记录了数据库的创建的库,创建的表,以及表里的内容全都有

11.下面创建用于主从相互同步数据的用户

12.先进入到mysql里面去

  • mysql -uroot -p123456
[root@xuexi-001 mysql]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 


13.创建用户

  • grant replication slave on . to 'repl'@'192.168.5.130' identified by '234567';
  • 指定权限,replication slave权限
  • 针对repl这个用户
  • 针对 从 的那个IP,指定来源(若是写所有的IP会很危险)
mysql> grant replication slave on *.* to 'repl'@'192.168.5.133' identified by '234567';
Query OK, 0 rows affected (0.00 sec)

mysql> 

14.锁定表,目的是不让表继续写,因为一会需要做 从 机器配置,需要进行一个同步,让两台机器同步,保证两台机器的数据一致,同步才不会出错

  • flush tables with read lock;
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> 

15.查看一下binlog的文件和大小,并记住binlog的filename

  • show master status;
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| xuexi1.000002 |    10558 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

16.然后退出数据库,做一个数据同步

17.查看/data/mysql/下有哪些库,主上有哪些库,一会从上也得有哪些库,同步这些库,就意味着这些数据都得备份过去

[root@xuexi-001 mysql]# ls 
auto.cnf     ib_logfile1         test           xuexi1.000001
db1          mysql               xuexi          xuexi1.000002
ibdata1      mysql2              xuexi-001.err  xuexi1.index
ib_logfile0  performance_schema  xuexi-001.pid  zrlog

18.备份数据库,除了mysql库,因为mysql库里面有账号密码,从上的时候不可能把所有权限复制过去,所以mysql不需要备份

  • 备份其他的库
[root@xuexi-001 mysql]# mysqldump -uroot -p123456 test > /tmp/test.sql
Warning: Using a password on the command line interface can be insecure.
[root@xuexi-001 mysql]# mysqldump -uroot -p123456 mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.

19.等会把/tmp/目录下 .sql文件都拷贝到 从上 去

[root@xuexi-001 mysql]# ls /tmp/*.sql
/tmp/mysql2.sql  /tmp/test.sql  /tmp/zrlog.sql

20.主上操作完成,接下来从上操作

配置从

  • 主从配置 - 从上操作
  • 安装mysql
  • 查看my.cnf,配置server-id=132,要求和主不一样
  • 修改完配置文件后,启动或者重启mysqld服务
  • 把主上aming库同步到从上
  • 可以先创建aming库,然后把主上的/tmp/mysql.sql拷贝到从上,然后导入aming库
  • mysql -uroot
  • stop slave;
  • change master to master_host='', master_user='repl', master_password='', master_log_file='', master_log_pos=xx,
  • start slave;
  • 还要到主上执行 unlock tables

主从配置 - 从上操作

  • 在从上机器配置

1.首先在从上安装并启动mysql,然后查看my.cnf,配置server_id=133,要求和主不一样,在配置文件的 log_bin参数 就不需要配置的,因为只有 主上 才需要二进制日志文件

[root@xuexi-001 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=133
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
 # log-error=/var/log/mariadb/mariadb.log
 # pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
# !includedir /etc/my.cnf.d
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M
保存退出

2.重启mysql服务

[root@xuexi-001 ~]#  /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL... SUCCESS! 

3.在增加 server-id 后,对于mysql 是没有任何变化的

[root@localhost ~]# ls /data/mysql/
auto.cnf     ib_logfile1                mysql
ibdata1      localhost.localdomain.err  performance_schema
ib_logfile0  localhost.localdomain.pid  test

4.把主机器上备份的 .sql 数据,拷贝到从机器上,然后做一个数据恢复

  • scp 192.168.202.130:/tmp/*.sql /tmp/
  • 拷贝数据失败
  • 原因:
  • 在拷贝的数据的时候,一直拷贝数据失败,因为在主上的机器里有开机启动脚本,所以导致拷贝数据失败
[root@localhost ~]# scp 192.168.5.130:/tmp/*.sql /tmp/
The authenticity of host '192.168.5.130 (192.168.5.130)' can't be established.
ECDSA key fingerprint is SHA256:VuRutO7q299bCFi40MmwIrojehhw6Uv2Z6Wm+QYfp6Q.
ECDSA key fingerprint is MD5:80:f0:db:83:55:d0:a5:6b:d8:e6:9a:22:bc:e6:2b:49.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added '192.168.5.130' (ECDSA) to the list of known hosts.
root@192.168.5.130's password: 
mysql2.sql                    100%  643KB  12.6MB/s   00:00    
test.sql                      100% 1258   236.9KB/s   00:00    
zrlog.sql                     100% 9869   770.5KB/s   00:00    
  • 解决方法:
    • 将主机器的里的开机启动脚本关闭vi .bashrc配置文件文件中注释掉脚本,再来拷贝数据到从机器上,会发现成功
[root@xuexi-001 ~]# scp 192.168.5.130:/tmp/*.sql /tmp/
The authenticity of host '192.168.5.130 (192.168.5.130)' can't be established.
ECDSA key fingerprint is 25:ce:a9:96:36:88:84:ab:4f:7e:80:a3:e7:36:2c:9f.
Are you sure you want to continue connecting (yes/no)? yes\
Warning: Permanently added '192.168.202.130' (ECDSA) to the list of known hosts.
root@192.168.5.130's password: 
test.sql                                                      100% 1258     1.2KB/s   00:00    
zrlog.sql                                                     100%   10KB   9.8KB/s   00:00    

5.进入到从上数据库中——>因为从上没设置密码,所以可以直接进去

[root@xuexi-001 ~]# mysql -uroot
-bash: mysql: 未找到命令
  • mysql -uroot命令不存在,是因为没有加入到PATH里面
  • 这里做个alias别名设置,用单引号' '
[root@xuexi-001 ~]# alias 'mysql=/usr/local/mysql/bin/mysql'
[root@xuexi-001 ~]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'
  • 这时候就可以进入到mysql里面去了
[root@xuexi-001 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

6.创建库

  • create database zrlog;
  • create database test1;
  • create database mysql2;
mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)

mysql> create database test1;
Query OK, 1 row affected (0.00 sec)

mysql> create database mysql2;
Query OK, 1 row affected (0.00 sec)
mysql> quit
Bye

退出数据库

7.然后将数据库做一个恢复

  • mysql -uroot test1 < /tmp/test.sql
  • mysql -uroot zrlog < /tmp/zrlog.sql
  • mysql -uroot mysql2 < /tmp/mysql2.sql
[root@localhost ~]# mysql -uroot test1 < /tmp/test.sql
[root@localhost ~]# mysql -uroot zrlog < /tmp/zrlog.sql
[root@localhost ~]# mysql -uroot mysql2 < /tmp/mysql2.sql 
  • 保证两边数据一致

  • 然后查看/data/mysql/目录下的数据是否和主机器上的/data/mysql/目录是否一致

8.开始实现主从

9.在从机器登录到mysql

[root@xuexi-001 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

10.然后在数据库里面执行命令,停止slave

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

mysql> 

11.配置主机器相关配置

  • change master to master_host='192.168.5.130', master_user='repl', master_password='234567', master_log_file='xuexi1.000002', master_log_pos=10558;
  • master_host='192.168.202.130',指定主机器host
  • master_user='repl',指定主机器用户
  • master_password='hanfeng',指定主机器密码
  • master_log_file='hanfeng1.000001',指定binlog文件名
  • master_log_pos=10549,指定binlog文件大小
  • 也可以指定主机器的port,因为在生产环境中,也会有人更改mysql的默认端口 master_port=3306
mysql> change master to master_host='192.168.5.130', master_user='repl', master_password='234567', master_log_file='xuexi1.000002', master_log_pos=10558;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

12.开始slave

  • start slave;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> 

13.这时候通过 show slave status\G 判断主从是否配置成功

  • 查看slave配置
  • show slave status\G
  • 在G后面不需要加分号,\G本身就是一种结束符
  • 看 Slave_IO_Running: Yes 是否为yes
  • 看 Slave_SQL_Running: Yes 是否为yes
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.130
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: xuexi1.000004
          Read_Master_Log_Pos: 120
               Relay_Log_File: localhost-relay-bin.000005
                Relay_Log_Pos: 280
        Relay_Master_Log_File: xuexi1.000004
             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: 120
              Relay_Log_Space: 617
              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: 130
                  Master_UUID: 9bace481-7ed7-11e8-98a4-000c29b3a2bf
             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 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)

mysql> 

14.解锁“主”上的表(在主上机器操作)

  • unlock tables;
[root@xuexi-001 ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

15.到这里主从搭建就算完成了

查看主从同步是否正常

  • 从上执行mysql -uroot
  • show slave stauts\G
  • 看是否有
  • Slave_IO_Running: Yes
  • Slave_SQL_Running: Yes
  • 还需关注
  • Seconds_Behind_Master: 0 //为主从延迟的时间
  • Last_IO_Errno: 0
  • Last_IO_Error:
  • Last_SQL_Errno: 0
  • Last_SQL_Error:

测试主从同步

主服务器上

binlog-do-db=            //仅同步指定的库(多个库,可以用“ , ”逗号分隔)——>英文的逗号 , 
binlog-ignore-db=     //忽略指定库

从服务器上

replicate_do_db=          //仅同步指定的库
replicate_ignore_db=     //忽略指定库
replicate_do_table=         //仅同步指定的表
replicate_ignore_table=    //忽略指定表,

    - 例如:有一个临时表,写的数据非常快,数据也大,每天都需要删除这时就可以更新删除这个,那么就不需要每天去做同步
 replicate_wild_do_table=   //如aming.%, 支持通配符%  指定同步靠谱的匹配  同步表   
replicate_wild_ignore_table=   //如aming.%, 支持通配符%  指定同步靠谱的匹配  忽略表
  • 进行从服务器的配置时尽量使用参数“replicate_wild_”,使匹配更精确,提升使用性能。

测试主从

  • 主上 mysql -uroot aming
  • select count(*) from db;
  • truncate table db;
  • 到从上 mysql -uroot aming
  • select count(*) from db;
  • 主上继续drop table db;
  • 从上查看db表

1.在主上机器,进入到mysql

[root@xuexi-001 ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5562
Server version: 5.6.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

2.进入到zrlog库里面

mysql> use zrlog;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 

3.查看表,有几个表

show tables;
mysql> show tables;
+-----------------+
| Tables_in_zrlog |
+-----------------+
| comment         |
| link            |
| log             |
| lognav          |
| plugin          |
| tag             |
| type            |
| user            |
| website         |
+-----------------+
9 rows in set (0.00 sec)

mysql> 

4.查看表有多少行,会看到website里面有9行数据

  • select count(*) from website;
mysql> select count(*) from website;
+----------+
| count(*) |
+----------+
|        9 |
+----------+
1 row in set (0.00 sec)

mysql> 

5.这时候再来查看 从上 的zrlog表上的数据,会看到是一样的


6.将主机器上的 表做一个删除操作

  • truncate table website;
  • truncate 表示 清空
mysql> truncate table website;
Query OK, 0 rows affected (0.04 sec)

7.再来查看 主机器 的表和 从机器 上表都会被删除了

mysql> select count(*) from website;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from website;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)
  • 若是误操作了,比如在从机器误删除了,再去主上删除相同的数据,就会有可能导致主从失败
  • 这时在从机器上 start slave;
  • 然后在start slave;
  • 再来查看show slave status\G
  • 若是还是失败,则只能 重新做主从了
  • 重新主从
  • 在主机器的数据库上 show mater status; 查看文件大小
  • 然后在从机器上先stop slave;
  • 然后直接change master to master_host='192.168.202.130', master_user='repl', master_password='hanfeng', master_log_file='hf123.000001', master_log_pos=10549;
  • 因为基本还没做什么操作的,数据还是一致的,直接改下数据大小就行
  • 然后在从机器上 start slave;
  • 再来查看 show slave status\G 看是否为两个Yes
  • 若实在只能从头做主从的
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!