第17章MySQL主從配置

mysql安裝總結

  • mysql主從準備工做:
  • 準備兩臺機器,每臺機器安裝msyql服務,並啓動mysql服務
  • mysql詳細安裝

1.首先下載二進制免編譯的包,下載到/usr/local/src/目錄下html

2.解壓壓縮包mysql

3.解壓完以後,把解壓出來的目錄放到 /usr/local/mysql/ 目錄下linux

  • 注意點:
  • 首先檢查 /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/ 目錄下,進行初始化 命令web

  • 初始化命令 ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  • 注意點:
  • 其中的--user=mysql 須要提早建立

5.初始化成功的標誌就是兩個OK,或者用 echo $? 檢查是否初始化成功sql

6.編輯 /etc/my.cnf 文件——>默認是自帶 my.cnf 文件的數據庫

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

7.拷貝啓動腳本vim

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

8.編輯啓動腳本bash

vim /etc/init.d/mysqld //對如下兩行進行指定路徑

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

9.以後就能夠啓動mysql了服務器

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

10.若是啓動失敗,能夠去查看錯誤日誌dom

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.這個是二進制日誌文件,會持續生成二、三、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
  • 若實在只能從頭作主從的
相關文章
相關標籤/搜索