MySQL主從介紹 , 準備工做,配置主,配置從, 測試主從同步

MySQL主從介紹

  • MySQL主從又叫作Replication、AB複製。簡單講就是A和B兩臺機器作主從後,在A上寫數據,另一臺B也會跟着寫數據,二者數據實時同步的
  • MySQL主從是基於binlog的,主上須開啓binlog才能進行主從。
  • 主從過程大體有3個步驟

1)主將更改操做記錄到binlog裏html

2)從將主的binlog事件(sql語句)同步到從本機上並記錄在relaylog裏mysql

3)從根據relaylog裏面的sql語句按順序執行linux

主上有一個log dump線程,用來和從的I/O線程傳遞binlogsql

從上有兩個線程,其中I/O線程用來同步主的binlog並生成relaylog,另一個SQL線程用來把relaylog裏面的sql語句落地服務器

準備工做

  • 準備兩臺服務器作主和從,兩臺服務器都要安裝mysql並啓動

配置主

  • 修改my.cnf,增長server-id=128和log_bin=aminglinux1
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=128
log_bin=aminglinux1

改完重啓服務socket

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

aminglinux1.index,aminglinux1.000001必定要有這兩個,這兩個很關鍵。主從要用到。ide

[root@aminglinux-01 ~]# ls -lt /data/mysql/
總用量 110636
-rw-rw----. 1 mysql mysql 50331648 11月  9 14:20 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 11月  9 14:20 ibdata1
-rw-rw----. 1 mysql mysql    17384 11月  9 14:20 aminglinux-01.err
-rw-rw----. 1 mysql mysql        5 11月  9 14:20 aminglinux-01.pid
-rw-rw----. 1 mysql mysql       21 11月  9 14:20 aminglinux1.index
-rw-rw----. 1 mysql mysql      120 11月  9 14:20 aminglinux1.000001
-rw-rw----. 1 mysql mysql       56 9月  27 09:52 auto.cnf
drwx------. 2 mysql mysql     4096 9月  26 21:00 mysql
drwx------. 2 mysql mysql     4096 9月  26 21:00 performance_schema
-rw-rw----. 1 mysql mysql 50331648 9月  26 21:00 ib_logfile1
drwx------. 2 mysql mysql        6 9月  26 21:00 test
[root@aminglinux-01 ~]#
  • 建立一個庫 mysql -uroot -paminglinux -e "create database aming"
[root@aminglinux-01 ~]# mysql -uroot -paminglinux -e "create database aming"
Warning: Using a password on the command line interface can be insecure.
  • 建立一個用戶,用來作主從同步數據的用戶

mysql -uroot -paminglinux 進入mysql學習

建立用戶 replication slave on . to 'repl'@slave_ip' identified by 'password';this

mysql> grant replication slave on *.* to 'repl'@'192.168.245.130' identified by 'aminglinux111';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'repl'@'192.168.245.130' identified by 'aminglinux111';
Query OK, 0 rows affected (0.00 sec)

mysql>

鎖一下,不讓主繼續寫了。讓二者保持一致。flush tables with read lock;.net

show master status;

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

到這個目錄下,坐下備份

[root@aminglinux-01 mysql]# cd /data/mysql/
[root@aminglinux-01 mysql]# ls
aming              aminglinux-01.pid   aminglinux1.000002  auto.cnf  ib_logfile0  mysql               test
aminglinux-01.err  aminglinux1.000001  aminglinux1.index   ibdata1   ib_logfile1  performance_schema
[root@aminglinux-01 mysql]#

ls /tmp/*sql 而後把這個目錄下全部的sql文件拷貝到從上去

配置從

  • 查看my.cnf,配置server-id=130,要求和主不同。logbin不須要配置了,只有主須要配置
  • 修改完配置文件後,啓動或者重啓mysqld服務
  • 能夠先建立aming庫
mysql> create database aming;
Query OK, 1 row affected (0.01 sec)
  • 而後把主上的庫文件capy到從上讓兩邊庫保持一致
  • 登錄從的mysql

stop slave;

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

實現主從很是關鍵的一部,change master to master_host='', master_user='repl', master_password='', master_log_file='', master_log_pos=xx,

下面的數值是以前主上show master status;獲得的

mysql> change master to master_host='192.168.245.128', master_user='repl', master_password='aminglinux111', master_log_file='aminglinux1.000002', master_log_pos=495;

start slave;

show slave status\G查看是否成功,兩個yes表明成功

Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

還要到主上執行 unlock tables

#擴展學習

相關文章
相關標籤/搜索