MySql 5.7.18 數(shù)據(jù)庫(kù)主從(Master/Slave)同步安裝與配置詳解

MySql復(fù)制的優(yōu)點(diǎn):
1.如果主服務(wù)器出現(xiàn)問(wèn)題,可以快速切換到從服務(wù)器提供的服務(wù)
2.可以在從服務(wù)器上執(zhí)行查詢操作,降低主服務(wù)器的訪問(wèn)壓力
3.可以在從服務(wù)器上執(zhí)行備份,以避免備份期間影響主服務(wù)器的服務(wù)

注意:一般只有更新不頻繁的數(shù)據(jù)或者對(duì)實(shí)時(shí)性要求不高的數(shù)據(jù)可以通過(guò)從服務(wù)器查詢,實(shí)時(shí)性要求高的數(shù)據(jù)仍然需要從主服務(wù)器獲得。

1、測(cè)試環(huán)境

操作系統(tǒng) :Windows 7 32位操作系統(tǒng)(安裝雙數(shù)據(jù)庫(kù)端口分別為3306、3308)
數(shù)據(jù)庫(kù)版本:MySQL 5.7.18
主機(jī)A:192.168.1.103 (Master)
主機(jī)B:192.168.1.103(Slave)

配置的設(shè)置同樣適用于Centos,Centos的配置文件/etc/my.cnf

service mysqld stop #停止數(shù)據(jù)庫(kù)
service mysqld start #啟動(dòng)數(shù)據(jù)庫(kù) 
service mysqld restart #重啟數(shù)據(jù)庫(kù)

2、數(shù)據(jù)庫(kù)安裝

可以參考之前寫(xiě)的文章:安裝MySql并修改初始密碼

這里貼一下Slave數(shù)據(jù)庫(kù)安裝的日志

Microsoft Windows [版本 6.1.7601]
版權(quán)所有 (c) 2009 Microsoft Corporation。保留所有權(quán)利。

C:\Windows\system32>cd C:\Program Files\mysql-5.7.18-win32-slave\bin

C:\Program Files\mysql-5.7.18-win32-slave\bin>mysqld  --initialize

C:\Program Files\mysql-5.7.18-win32-slave\bin>cd  ../data

C:\Program Files\mysql-5.7.18-win32-slave\data>TYPE Javen-PC.err
2017-06-29T02:41:51.068120Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is
 deprecated. Please use --explicit_defaults_for_timestamp server option (see doc
umentation for more details).
2017-06-29T02:41:51.573560Z 0 [Warning] InnoDB: New log files created, LSN=45790

2017-06-29T02:41:51.643760Z 0 [Warning] InnoDB: Creating foreign key constraint
system tables.
2017-06-29T02:41:51.699920Z 0 [Warning] No existing UUID has been found, so we a
ssume that this is the first time that this server has been started. Generating
a new UUID: 818f5c2f-5c74-11e7-8dff-000c29b2597f.
2017-06-29T02:41:51.699920Z 0 [Warning] Gtid table is not ready to be used. Tabl
e 'mysql.gtid_executed' cannot be opened.
2017-06-29T02:41:51.713960Z 1 [Note] A temporary password is generated for root@
localhost: =rc%=eBVg0AY

C:\Program Files\mysql-5.7.18-win32-slave\data>cd ..

C:\Program Files\mysql-5.7.18-win32-slave>cd bin

C:\Program Files\mysql-5.7.18-win32-slave\bin>mysqld -install MySQL2
Service successfully installed.

C:\Program Files\mysql-5.7.18-win32-slave\bin>net start MySQL2
MySQL2 服務(wù)正在啟動(dòng) .
MySQL2 服務(wù)已經(jīng)啟動(dòng)成功。

C:\Program Files\mysql-5.7.18-win32-slave\bin>mysql -u root -p=rc%=eBVg0AY -P3308

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18

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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT
 OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH  PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

1、在mysql-5.7.18-win32-slave根目錄中創(chuàng)建my.ini初始化配置文件

[mysqld]
# set basedir to your installation path
basedir=C:\\Program Files\\mysql-5.7.18-win32-slave
# set datadir to the location of your data directory
datadir=C:\\Program Files\\mysql-5.7.18-win32-slave\\data
port = 3308

2、進(jìn)入bin目錄mysqld --initialize初始化數(shù)據(jù)庫(kù)文件
3、TYPE Javen-PC.err 查看處理初始化密碼。Javen-PC.err 是你電腦的名稱 ,這里的初始化密碼為=rc%=eBVg0AY
4、注冊(cè)mysql服務(wù) mysqld -install MySQL2 **
5、啟動(dòng)服務(wù)
net start MySQL2**
6、登錄本地mysql:mysql -u root -p=rc%=eBVg0AY -P3308
7、修改本地root用戶密碼

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';

8、授權(quán)遠(yuǎn)程登錄(需要關(guān)閉防火墻或者配置指定端口可以訪問(wèn))

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

FLUSH  PRIVILEGES;

3、配置主服務(wù)器Master

3.1 給從服務(wù)器設(shè)置授權(quán)用戶(創(chuàng)建復(fù)制帳號(hào))

建立一個(gè)帳戶javen,并且只能允許從192.168.1.103這個(gè)地址上來(lái)登陸,密碼是123456。

mysql> grant replication slave on *.* to 'javen'@'192.168.1.103' identified by '123456';
mysql> flush privileges;

3.2 主服務(wù)器Master配置

[mysqld]
# set basedir to your installation path
basedir=C:\\Program Files\\mysql-5.7.18-win32
# set datadir to the location of your data directory
datadir=C:\\Program Files\\mysql-5.7.18-win32\\data
port = 3306

log-bin           = mysql-bin   #[必須]啟用二進(jìn)制日志
server-id         = 1           #[必須]服務(wù)器唯一ID,默認(rèn)是1
expire-logs-days  = 7           #只保留7天的二進(jìn)制日志,以防磁盤(pán)被日志占滿
#replicate-do-db   = test        #需要做復(fù)制的數(shù)據(jù)庫(kù)名;這里不設(shè)置只配置備份的數(shù)據(jù)庫(kù)
binlog-ignore-db  = mysql       #不備份的數(shù)據(jù)庫(kù)
binlog-ignore-db  = information_schema
binlog-ignore-db  = performation_schema
binlog-ignore-db  = sys
#binlog-do-db=test #需要做復(fù)制的數(shù)據(jù)庫(kù)名

3.3 重啟MySQL服務(wù)并設(shè)置讀取鎖定

net stop MySQL
net start MySQL

在主服務(wù)器上設(shè)置讀取鎖定有效,確保沒(méi)有數(shù)據(jù)庫(kù)操作,以便獲得一個(gè)一致性的快照

mysql -u root -proot -P3306
mysql> flush tables with read lock;

3.4 查看主服務(wù)器上當(dāng)前的二進(jìn)制日志名和偏移量值

mysql> show master status;
+------------------+----------+--------------+----------------------------------
----------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB
                | Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------
----------------+-------------------+
| mysql-bin.000001 |     2519 |              | mysql,information_schema,performa
tion_schema,sys |                   |
+------------------+----------+--------------+----------------------------------
----------------+-------------------+
1 row in set (0.00 sec)

這里的 File 、Position 是在配置Salve的時(shí)候要使用到的,Binlog_Do_DB表示要同步的數(shù)據(jù)庫(kù),Binlog_Ignore_DB 表示Ignore的數(shù)據(jù)庫(kù),這些都是在配置的時(shí)候進(jìn)行指定的。

另外:如果執(zhí)行這個(gè)步驟始終為Empty set(0.00 sec),那說(shuō)明前面的my.init沒(méi)配置對(duì)。

4、配置從服務(wù)器Slave

4.1 修改從數(shù)據(jù)庫(kù)的配置

[mysqld]
# set basedir to your installation path
basedir=C:\\Program Files\\mysql-5.7.18-win32-slave
# set datadir to the location of your data directory
datadir=C:\\Program Files\\mysql-5.7.18-win32-slave\\data
port = 3308


log-bin=mysql-bin
server-id=3
binlog-ignore-db  = mysql       #不備份的數(shù)據(jù)庫(kù)
binlog-ignore-db  = information_schema
binlog-ignore-db  = performation_schema
binlog-ignore-db  = sys
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60

4.2 重啟從數(shù)據(jù)庫(kù)并設(shè)置Slave數(shù)據(jù)庫(kù)

net stop MySQL2
net start MySQL2

登錄從數(shù)據(jù)庫(kù)并做如下設(shè)置

mysql> stop slave;  #關(guān)閉Slave
mysql> change master to master_host='192.168.1.103',master_user='javen',master_password='123456',master_log_file='mysql-bin.000001', master_log_pos= 2519;

mysql> start slave;  #開(kāi)啟Slave

注意:在這里指定Master的信息,master_log_file是在配置Master的時(shí)候的File選項(xiàng), master_log_pos是在配置Master的Position 選項(xiàng),這里要進(jìn)行對(duì)應(yīng)。

4.3 查看Slave配置的信息

show slave status 查看配置的信息:

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.103
                  Master_User: mysync
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1192
               Relay_Log_File: Javen-PC-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: mysql
           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: 1192
              Relay_Log_Space: 530
              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: 1
                  Master_UUID: 818f5c2f-5c74-11e7-8dff-000c29b2597f
             Master_Info_File: C:\Program Files\mysql-5.7.18-win32-slave\data\ma
ster.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more up
dates
           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)

ERROR:
No query specified

mysql>

5、關(guān)閉掉主數(shù)據(jù)庫(kù)的讀取鎖定

mysql> unlock tables;

6、在主數(shù)據(jù)庫(kù)中創(chuàng)建一個(gè)表以及添加數(shù)據(jù)測(cè)試

在主數(shù)據(jù)庫(kù)(Master)添加一個(gè)test數(shù)據(jù)庫(kù)并在其中添加t1的數(shù)據(jù)表。如下圖

Master數(shù)據(jù)庫(kù)

在從數(shù)據(jù)庫(kù)(Slave)自動(dòng)同步,如下圖

Slave數(shù)據(jù)庫(kù)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,963評(píng)論 6 542
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,348評(píng)論 3 429
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 178,083評(píng)論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 63,706評(píng)論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,442評(píng)論 6 412
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 55,802評(píng)論 1 328
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,795評(píng)論 3 446
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 42,983評(píng)論 0 290
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,542評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,287評(píng)論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,486評(píng)論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,030評(píng)論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,710評(píng)論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 35,116評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 36,412評(píng)論 1 294
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,224評(píng)論 3 398
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,462評(píng)論 2 378

推薦閱讀更多精彩內(nèi)容