數據庫水平切分(MySQL主從複製)

1安裝MySQLjava

新的centos系統安裝mysql依賴:mysql

  1. perl perl-Module-Build
  2. autoconf
  3. libaio
  4. namuctl-libs
  5. net-tools
# yum install -y perl perl-Module-Build net-tools autoconf libaio numactl-libs

Mysql 安裝完畢後密碼文件會在.mysql_secretsql

啓動mysql數據庫

# systemctl start mysql
# mysql -uroot -p密碼

設置mysql初始密碼:centos

mysql> set password = password(‘你的密碼’)

遠程登錄受權socket

mysql> grant all privileges on *.* to 'root'@'%' identified by 'abcd_123' with grant option;
mysql> flush privileges;

 

二進制日誌+中繼日誌tcp

Binlog(二進制日誌,阿里otter/canal)ide

MySQL日誌文件,裏面記錄DDL和DML(查詢語句除外)工具

 

Relay-log(中繼日誌)ui

主要用於同步的中間過程。

mysql配置文件my.cnf

[client]
port                        = 3306
socket                      = /var/lib/mysql/mysql.sock

[mysqld]
server-id                    = 2 #不管主機仍是從機server-id不能相同
port                        = 3306 #數據庫端口
datadir                      = /var/lib/mysql
socket                       = /var/lib/mysql/mysql.sock
default-storage-engine          = InnoDB
log-bin                      = mysql-bin    #二進制日誌名稱
log-bin-index                 = mysql-bin.index  #二進制日誌索引
relay-log                    = mysql-relay    #中繼日誌名稱
relay-log-index               = mysql-relay.index #索引
expire-logs-days              = 10
max-binlog-size              = 100M
max_binlog_cache_size        = 8M
log-slave-updates             = 1
binlog_cache_size            = 4M

# use MIXED binlog
binlog_format               = MIXED
#binlog_format              = ROW

#replicate-do-db             = db%.%
#replicate-ignore-db          = mysql.%

# ignore tables
replicate-wild-ignore-table     = mysql.%
sync_binlog                 = 1
relay_log_recovery           = 1
log_slave_updates           = 1
skip-name-resolve

sql_mode=STRICT_TRANS_TABLES

[mysqldump]
quick
max_allowed_packet = 32M

  

查詢mysql用戶列表

mysql> use mysql
mysql> select user,password,host from user;
+------+-------------------------------------------+--------------+
| user | password                                  | host         |
+------+-------------------------------------------+--------------+
| root | *1D7781F716B5315E62B337DE530D7B74CFEB52D3 | localhost    |
| root |                                           | 0dd980a68cce |
| root |                                           | 127.0.0.1    |
| root |                                           | ::1          |
|      |                                           | localhost    |
|      |                                           | 0dd980a68cce |
| root | *1D7781F716B5315E62B337DE530D7B74CFEB52D3 | %            |
+------+-------------------------------------------+--------------+
7 rows in set (0.00 sec)

 

主從複製過程(在主機上操做):

1複製my.cnf

cp /usr/share/mysql/my-default.cnf /etc/my.cnf

2建立同步複製的用戶  %爲通配符

mysql> create user 'repl'@'172.17.0.%' identified by 'abcd_123';

3給同步複製用戶賦權

mysql> grant replication slave on *.* to 'repl'@'172.17.0.%' identified by 'abcd_123';
mysql> flush privileges;

開啓binlog

配置時候注意幾個坑:
Replication-do-db的坑,若是多個庫則使用多行Replication-do-db進行配置
Replication-ignore-db的坑,若是忽略多個庫則使用多行Replication-ignore-db進行配置

重啓mysql

#sysctemctl restart mysql
#service mysql restart

查看日誌事件

mysql> show binlog events;
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                  |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| mysql-bin.000001 |   4 | Format_desc |         2 |         120 | Server ver: 5.6.41-log, Binlog ver: 4 |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
1 row in set (0.00 sec)
或者
mysql> show binlog events in 'mysql-bin.000001' from 4;
mysql> show binlog events in 'mysql-bin.000001' from 4\G;

從機操做

1複製my.cnf 修改server-id,重啓mysql

2主從複製的最關鍵語句:

Stop slave;
Change master to

         Master_host='172.17.0.2',

         Master_user='repl',

         Master_password='abcd_123',

         Master_log_file='mysql-bin.000001',

         Master_log_pos=4;
Start slave;

查看鏈接狀態

mysql> show slave status\G;

主機

mysql>  show processlist;

執行

# netstat -natp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp6       0      0 :::3306                 :::*                    LISTEN      900/mysqld          
tcp6       0      0 172.17.0.2:3306         172.17.0.3:41160        ESTABLISHED 900/mysqld  


 

關於主主複製:

Ip 172.17.0.2 master1

Ip 172.17.0.3 master2

先要修改2臺機器/etc/my.cnf

Master1上執行

建立同步複製的用戶

mysql> create user 'repl'@'172.17.0.%' identified by 'abcd_123';

給同步複製用戶賦權

mysql> grant replication slave on *.* to 'repl'@'172.17.0.%' identified by 'abcd_123';
mysql> flush privileges;

開啓binlog

配置時候注意幾個坑:
Replication-do-db的坑,若是多個庫則使用多行Replication-do-db進行配置
Replication-ignore-db的坑,若是忽略多個庫則使用多行Replication-ignore-db進行配置

重啓mysql

#sysctemctl restart mysql

Master2上執行

主從複製的最關鍵語句:

Stop slave;
Change master to
         Master_host=’172.17.0.2’,
         Master_user=’repl’,
         Master_password=’abcd_123’,
         Master_log_file=’mysql-bin.000001’,
         Master_log_pos=120;
Start slave;

Master2上執行

建立同步複製的用戶

mysql> create user 'repl'@'172.17.0.%' identified by 'abcd_123';

給同步複製用戶賦權

mysql> grant replication slave on *.* to 'repl'@'172.17.0.%' identified by 'abcd_123';
mysql> flush privileges;

開啓binlog

配置時候注意幾個坑:
Replication-do-db的坑,若是多個庫則使用多行Replication-do-db進行配置
Replication-ignore-db的坑,若是忽略多個庫則使用多行Replication-ignore-db進行配置

重啓mysql

#sysctemctl restart mysql

Master1上執行

Stop slave;
Change master to
         Master_host=’172.17.0.3’,
         Master_user=’repl’,
         Master_password=’abcd_123’,
         Master_log_file=’mysql-bin.000001’,
         Master_log_pos=120;
Start slave;

查看主機狀態命令:

mysql> show master status \G;

查看從機狀態命令:

mysql> show slave status \G;

查看mysql線程列表:

mysql> processlist;

查看二進制日誌:

mysql> show binlog events in ‘日誌文件’ from 位置;

查看中繼日誌:

mysql> show relaylog events in ‘日誌文件’ from 位置;

使用mysql工具查看日誌文件

# mysqlbinlog --start-position=位置 日誌路徑

# mysqlbinlog -start-position=120 /var/lib/mysql/mysql-bin.000001
相關文章
相關標籤/搜索