mysql主從同步分庫分表同步

來源:互聯網
上載者:User

標籤:mysql

一、mysql資料庫的安裝

分別在master 和slave上源碼安裝mysql資料庫

1.1 安裝相關包
1.1.1 cmake軟體
cd /home/oldboy/tools/
tar xf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure
#CMake has bootstrapped.  Now run gmake.
gmake
gmake install
cd ../

1.1.2 依賴包
yum install ncurses-devel -y

1.2 開始安裝mysql
1.2.1 建立使用者和組
groupadd mysql
useradd mysql -s /sbin/nologin -M -g mysql

1.2.2 解壓編譯MySQL
tar zxf mysql-5.5.32.tar.gz
cd mysql-5.5.32
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql/mysql-5.5.40 \
-DMYSQL_DATADIR=/application/mysql/mysql-5.5.40/data \
-DMYSQL_UNIX_ADDR=/application/mysql/mysql-5.5.40/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0

#-- Build files have been written to: /home/oldboy/tools/mysql-5.5.32
提示,編譯時間可配置的選項很多,具體可參考結尾附錄或官方文檔:
make
#[100%] Built target my_safe_process
make install
ln -s /application/mysql-5.5.32/ /application/mysql
如果上述操作未出現錯誤,則MySQL5.5.32軟體cmake方式的安裝就算成功了。

1.3安裝資料庫成功後

1.3.1 修改mysql安裝目錄的許可權,全部修改為mysql

chown -R mysql:mysql /application/mysql

1.3.2 初始化資料庫

cd /application/mysql/scripts

[[email protected] scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/mysql --user=mysql

1.3.3 替換/etc/下的設定檔和資料庫的開機檔案

/application/mysql/support-files

cp -r my-small.cnf  /etc/my.cnf

cp -r mysql.server  /etc/init.d/mysql

1.4啟動資料庫

/etc/init.d/mysql  start

/etc/init.d/mysql  stop

/etc/init.d/mysql  restart

二、mysql資料庫分庫分表的同步

2.1master設定檔

log-bin=mysql-bin

binlog_format=row

server-id       = 15

binlog-do-db=sales

/etc/init.d/mysql  restart(重啟資料庫)

2.2slave設定檔

server-id       = 25

relay-log=relay-bin
read-only = 1
replicate-do-db = sales

replicate-do-db =user_info

replicate-ignore-db = information_schema
replicate-ignore-db = mysql
replicate-ignore-db = user_info

replicate-wild-do-table =sales.story    // 所要同步的資料庫的單個表

/etc/init.d/mysql  restart(重啟資料庫)

2.3在master上建立同步的使用者
 grant replication client,replication slave on *.* to [email protected]‘172.27.1.%‘ identified by ‘password‘;

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 150
     Binlog_Do_DB: sales,user_info
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

ERROR:
No query specified

2.4在slave上執行如下語句,同步到master

change master to master_host=‘172.27.1.12‘(主庫IP地址),master_user=‘rep‘,master_password=‘financial‘,master_log_file=‘mysql-bin.000003‘,master_log_pos=150;

start slave(啟動從庫)

stop slave(停止從庫)

reset slave(重設從庫)

啟動資料庫後查看slave的狀態如下:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.27.1.12
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 150
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 269
        Relay_Master_Log_File: mysql-bin.001603
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: sales
          Replicate_Ignore_DB: information_schema,mysql,user_info
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table: sales.story
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 150
              Relay_Log_Space: 419
              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: 15
1 row in set (0.00 sec)

ERROR:
No query specified

上述表示資料庫master and slave 已配置好,可以進行同庫單個表的配置


測試:

1.分別在master和slave建立資料庫sales(row模式下需要手動建立資料庫)。

2.use sales庫下,在master上建立表

CREATE TABLE user_info (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

3.在master上建立表

CREATE TABLE story (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

查看slave同步狀態:

a. story表同步成功

b. user_info表同步不成功


則mysql資料庫單庫,單表同步成功。

如果只是單庫同步,則在slave的設定檔中去掉:

replicate-wild-do-table =sales.story    // 所要同步的資料庫的單個表


在單表同步中當出現(高版本5.6.2及以上)master----(低版本5.6.2以下)slave

啟動slave後,會報如下的錯誤:

[ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: ‘Slave can not handle replication events with the checksum that master is configured to log;


後查詢資料後發現:在5.6.2中,新增了一個參數binlog_checksum,而且在5.6.6及以後的版本中,該參數的值由NONE改為了CRC32,master為5.6.30的情況下,自然有該選項參數(為了驗證binlog events的正確,指定通過CRC32演算法來避免出現events corrupt,所以這個選項參數出現了。),並且其值為CRC32,但slave為5.5.32,此時的該選項參數是不存在的。

【解決方案】:
直接在master上執行,即可:

  1. mysql> SET GLOBAL binlog_checksum=none;

當然也要加到設定檔的[mysqld]中,避免重啟失效:

  1. [mysqld]

  2. binlog-checksum = none


注意:在該參數值還未修改之前的binlog,似乎也是無法被io thread所接收的,故之前的binlog可能需要手動執行來補資料了。
在修改之後的binlog中的events,則可以被正確的傳輸到slave上。









本文出自 “貓咪” 部落格,請務必保留此出處http://yanruohan.blog.51cto.com/9740053/1923245

mysql主從同步分庫分表同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.