MySQL主從復制(傳統(tǒng))

拓撲結(jié)構(gòu):(一主多從)

Mysql-master01? (192.168.43.123)

Mysql-slave01? ? ?(192.168.43.124)

Mysql-slave02? ? ?(192.168.43.125)

版本信息:

OS版本? ? ? : CentOS release 6.10 最小化安裝

Mysql版本: mysql-5.7.31-1.el6.x86_64.rpm-bundle.tar


一赦役、系統(tǒng)調(diào)整(測試)

適用范圍:所有服務器

1.關(guān)閉防火墻

2.修改/etc/selinux/config關(guān)閉selinux

3.其他工具軟件的安裝 vim openssh-* net-tools 等


二魂贬、Mysql的安裝

適用范圍:所有服務器

1.安裝

[root@localhost opt]# tar -xf mysql-5.7.31-1.el6.x86_64.rpm-bundle.tar

[root@localhost opt]# yum install mysql-community-* -y

2.root密碼調(diào)整

[root@localhost opt]# /etc/init.d/mysqld start

Initializing MySQL database:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]

Starting mysqld:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]

[root@localhost opt]#

mysql> alter user 'root'@'localhost' identified by 'Test.123';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


三、配置主從復制(傳統(tǒng))

1.配置調(diào)整

主服務器配置:

[root@localhost opt]# mkdir /var/log/mysql

[root@localhost opt]# id mysql

uid=27(mysql) gid=27(mysql) groups=27(mysql)

[root@localhost opt]# chown mysql.mysql /var/log/mysql

[root@localhost opt]# cat /etc/my.cnf

[mysqld]

log-bin=/var/log/mysql/mysql-bin

server-id=1

innodb_flush_log_at_trx_commit = 1

sync_binlog = 1

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

symbolic-links=0

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

default-storage-engine = INNODB

character-set-server = utf8

collation-server = utf8_general_ci

[client]

default-character-set = utf8

[root@localhost opt]# /etc/init.d/mysqld restart

Stopping mysqld:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]

Starting mysqld:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]

[root@localhost opt]#

從服務器配置:

[root@localhost opt]# mkdir /var/log/mysql

[root@localhost opt]# id mysql

uid=27(mysql) gid=27(mysql) groups=27(mysql)

[root@localhost opt]# chown mysql.mysql /var/log/mysql

[root@localhost opt]# cat /etc/my.cnf

[mysqld]

log-bin=/var/log/mysql/mysql-bin

server-id=2

innodb_flush_log_at_trx_commit = 1

sync_binlog = 1

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

symbolic-links=0

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

default-storage-engine = INNODB

character-set-server = utf8

collation-server = utf8_general_ci

[client]

default-character-set = utf8

[root@localhost opt]# /etc/init.d/mysqld restart

Stopping mysqld:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]

Starting mysqld:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]

[root@localhost opt]#

2.復制用戶創(chuàng)建(主服務器配置)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' identified by 'Test.123';

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql>

從服務器測試一下:

[root@localhost opt]# mysql -urepl -p'Test.123' -h 192.168.43.123

mysql: [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.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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>

3.將從服務器加入新服務器(2臺從服務器)

[root@localhost opt]# mysql -uroot -p'Test.123'

mysql: [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 3

Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> system clear

mysql>? FLUSH TABLES WITH READ LOCK;

Query OK, 0 rows affected (0.00 sec)

mysql> show master status \G

*************************** 1. row ***************************

? ? ? ? ? ? File: mysql-bin.000001

? ? ? ? Position: 154

? ? Binlog_Do_DB:

Binlog_Ignore_DB:

Executed_Gtid_Set:

1 row in set (0.00 sec)

mysql> CHANGE MASTER TO MASTER_HOST='192.168.43.123', MASTER_USER='repl', MASTER_PASSWORD='Test.123', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;

Query OK, 0 rows affected, 2 warnings (0.03 sec)

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show master status \G

*************************** 1. row ***************************

? ? ? ? ? ? File: mysql-bin.000001

? ? ? ? Position: 154

? ? Binlog_Do_DB:

Binlog_Ignore_DB:

Executed_Gtid_Set:

1 row in set (0.00 sec)

mysql> show slave status \G

*************************** 1. row ***************************

? ? ? ? ? ? ? Slave_IO_State: Waiting for master to send event

? ? ? ? ? ? ? ? ? Master_Host: 192.168.43.123

? ? ? ? ? ? ? ? ? Master_User: repl

? ? ? ? ? ? ? ? ? Master_Port: 3306

? ? ? ? ? ? ? ? Connect_Retry: 60

? ? ? ? ? ? ? Master_Log_File: mysql-bin.000001

? ? ? ? ? Read_Master_Log_Pos: 872

? ? ? ? ? ? ? Relay_Log_File: localhost-relay-bin.000002

? ? ? ? ? ? ? ? Relay_Log_Pos: 320

? ? ? ? Relay_Master_Log_File: mysql-bin.000001

? ? ? ? ? ? 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: 154

? ? ? ? ? ? ? Relay_Log_Space: 1249

? ? ? ? ? ? ? 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: 471

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: 1

? ? ? ? ? ? ? ? ? Master_UUID: 04baf52c-e083-11ea-9564-000c29298862

? ? ? ? ? ? Master_Info_File: /var/lib/mysql/master.info

? ? ? ? ? ? ? ? ? ? SQL_Delay: 0

? ? ? ? ? SQL_Remaining_Delay: NULL

? ? ? Slave_SQL_Running_State: Waiting for global read lock

? ? ? ? ? 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

? ? ? ? Replicate_Rewrite_DB:

? ? ? ? ? ? ? ? Channel_Name:

? ? ? ? ? Master_TLS_Version:

1 row in set (0.00 sec)

mysql> show binlog events\G;

*************************** 1. row ***************************

? Log_name: mysql-bin.000001

? ? ? ? Pos: 4

Event_type: Format_desc

? Server_id: 2

End_log_pos: 123

? ? ? Info: Server ver: 5.7.31-log, Binlog ver: 4

*************************** 2. row ***************************

? Log_name: mysql-bin.000001

? ? ? ? Pos: 123

Event_type: Previous_gtids

? Server_id: 2

End_log_pos: 154

? ? ? Info:

2 rows in set (0.00 sec)

ERROR:

No query specified

mysql>

mysql> unlock tables;

Query OK, 0 rows affected (0.01 sec)


四诱鞠、測試

主服務器操作:

mysql> create database testrep;

Query OK, 1 row affected (0.00 sec)

mysql> use testrep;

Database changed

mysql> create table t(id int primary key,name varchar(20));

Query OK, 0 rows affected (0.02 sec)

mysql> desc t;

+-------+-------------+------+-----+---------+-------+

| Field | Type? ? ? ? | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id? ? | int(11)? ? | NO? | PRI | NULL? ? |? ? ? |

| name? | varchar(20) | YES? |? ? | NULL? ? |? ? ? |

+-------+-------------+------+-----+---------+-------+

2 rows in set (0.04 sec)

mysql> insert into t values(1,'Pony');

Query OK, 1 row affected (0.01 sec)

mysql> insert into t values(2,'Allen');

Query OK, 1 row affected (0.00 sec)

mysql> insert into t values(3,'Peter');

Query OK, 1 row affected (0.00 sec)

mysql> select * from t;

+----+-------+

| id | name? |

+----+-------+

|? 1 | Pony? |

|? 2 | Allen |

|? 3 | Peter |

+----+-------+

3 rows in set (0.00 sec)

mysql>

從服務器查詢:

mysql> show databases;

+--------------------+

| Database? ? ? ? ? |

+--------------------+

| information_schema |

| mysql? ? ? ? ? ? ? |

| performance_schema |

| sys? ? ? ? ? ? ? ? |

| testrep? ? ? ? ? ? |

+--------------------+

5 rows in set (0.00 sec)

mysql> use testrep;

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> show tables;

+-------------------+

| Tables_in_testrep |

+-------------------+

| t? ? ? ? ? ? ? ? |

+-------------------+

1 row in set (0.00 sec)

mysql> select * from t;

+----+-------+

| id | name? |

+----+-------+

|? 1 | Pony? |

|? 2 | Allen |

|? 3 | Peter |

+----+-------+

3 rows in set (0.00 sec)


附:binlog日志轉(zhuǎn)sql文件

[root@localhost mysql]# mysqlbinlog --no-defaults --base64-output=decode-rows -v? mysql-bin.000001 --result-file=1.sql

備注:如果在配置主從復制之前互例,主服務器存在前期數(shù)據(jù)奢入,需要同步到從服務器,保持主從服務器數(shù)據(jù)一致后再進行同步操作媳叨。此間腥光,可用同步的工具可選用mysqldump、冷備份糊秆、xtrabackup等工具武福,這其中又需要考慮是MyISAM表還是InnoDB表。


參閱:

http://www.reibang.com/p/faf0127f1cb2

https://www.cnblogs.com/f-ck-need-u/p/9155003.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
禁止轉(zhuǎn)載痘番,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者捉片。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市汞舱,隨后出現(xiàn)的幾起案子伍纫,更是在濱河造成了極大的恐慌,老刑警劉巖昂芜,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件莹规,死亡現(xiàn)場離奇詭異,居然都是意外死亡说铃,警方通過查閱死者的電腦和手機访惜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門嘹履,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人债热,你說我怎么就攤上這事砾嫉。” “怎么了窒篱?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵焕刮,是天一觀的道長。 經(jīng)常有香客問我墙杯,道長配并,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任高镐,我火速辦了婚禮溉旋,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘嫉髓。我一直安慰自己观腊,他們只是感情好,可當我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布算行。 她就那樣靜靜地躺著梧油,像睡著了一般。 火紅的嫁衣襯著肌膚如雪州邢。 梳的紋絲不亂的頭發(fā)上儡陨,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天,我揣著相機與錄音量淌,去河邊找鬼骗村。 笑死,一個胖子當著我的面吹牛类少,可吹牛的內(nèi)容都是我干的叙身。 我是一名探鬼主播渔扎,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼硫狞,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了晃痴?” 一聲冷哼從身側(cè)響起残吩,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎倘核,沒想到半個月后泣侮,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡紧唱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年活尊,在試婚紗的時候發(fā)現(xiàn)自己被綠了隶校。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡蛹锰,死狀恐怖深胳,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情铜犬,我是刑警寧澤舞终,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站癣猾,受9級特大地震影響敛劝,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜纷宇,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一夸盟、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧像捶,春花似錦满俗、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至痘儡,卻和暖如春辕万,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背沉删。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工渐尿, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人矾瑰。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓砖茸,卻偏偏與公主長得像,于是被迫代替她去往敵國和親殴穴。 傳聞我的和親對象是個殘疾皇子凉夯,可洞房花燭夜當晚...
    茶點故事閱讀 45,691評論 2 361