mysql配置主從數(shù)據(jù)庫

mysql配置主從數(shù)據(jù)庫

MySQL的主從同步是一個很成熟的架構(gòu),優(yōu)點為:

①在從服務(wù)器可以執(zhí)行查詢工作(即我們常說的讀功能),降低主服務(wù)器壓力;

②在從主服務(wù)器進(jìn)行備份,避免備份期間影響主服務(wù)器服務(wù);

③當(dāng)主服務(wù)器出現(xiàn)問題時,可以切換到從服務(wù)器。

所以我在項目部署和實施中經(jīng)常會采用這種方案.

#我們在配置之前兩個服務(wù)器的數(shù)據(jù)庫不相同的話我們可以先遷移數(shù)據(jù)庫是兩邊的數(shù)據(jù)先相同!然后在配置主從

+遷移數(shù)據(jù)庫

mysqldump-h127.0.0.1-uroot-ppass myweb|mysql--host=***.***.***.*** -u數(shù)據(jù)庫用戶名 -p數(shù)據(jù)庫密碼 -C serweb

#將數(shù)據(jù)庫轉(zhuǎn)移到新服務(wù)器。此例為將本地數(shù)據(jù)庫myweb復(fù)制到遠(yuǎn)程數(shù)據(jù)庫名為serweb中,其中遠(yuǎn)程數(shù)據(jù)庫必須有名為serweb的數(shù)據(jù)庫

#在遷移過程中可能會報以下錯誤

mysqldump: Got error: 1045: Access denied for user 'csfdp'@'%' (using password: YES)

這時我們只要給用戶權(quán)限就好

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

使兩邊的數(shù)據(jù)相同后我們就可以配置主從同步了。

+ 數(shù)據(jù)庫目錄及其它

my.cnf配置文件 ????/etc/my.cnf

mysql數(shù)據(jù)庫位置 ???datadir=/usr/local/nginx/mysql

主數(shù)據(jù)庫:192.168.253.130

從數(shù)據(jù)庫:192.168.253.130

一、設(shè)置主庫

1、修改主庫my.cnf,主要是設(shè)置個不一樣的id和logbin(為了方便識別id我們可以設(shè)置為域名最后幾位數(shù)字)

[root@localhost etc]#vi /etc/my.cnf

# 記住這部分一定要配置在[mysqld]后面,否則無法找到從節(jié)點,各個配置項的含義可自己查閱文檔

log-bin=mysql-bin

binlog_format=mixed

server-id = 130

2、顯示主庫信息

mysql> show master status;

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

| File? ? ? ? ? ? | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000045 |? ? 2575 |? ? ? ? ? ? ? |? ? ? ? ? ? ? ? ? |

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

# 看到這個信息說明mysql的二進(jìn)制文件是從000045開始 ?磁盤的偏移量在2575

二、設(shè)置從庫

1、在220節(jié)點上修改從庫my.cnf(位置一樣)

log-bin=mysql-bin(開啟二進(jìn)制日志!根據(jù)個人習(xí)慣可開啟或不開啟,本人建議還是開啟,以防數(shù)據(jù)丟失時可以恢復(fù))

relay-log=mysql-relay(從庫的relay日志)

binlog_format=mixed()

server-id = 131

三、授權(quán)

#我們配置完主庫和從庫以后!我們要給兩個服務(wù)器簡歷鏈接!所以我們要授權(quán),出庫給從庫授權(quán)一個user從庫使用這個user建立起鏈接

1、grant replication slave on *.* to 'username'@'%' identified by 'your_password';

三、這是回到從庫設(shè)置

1、mysql> change master to ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? ? master_host='192.168.253.130',

? ? ? ? ? ? ? ? ? ? master_user='username',

? ? ? ? ? ? ? ? ? ? master_password='your_password',

? ? ? ? ? ? ? ? ? ? master_log_file='mysql-bin.000045',master_log_pos=2575;

2、開啟

mysql>start slave;

3、查看從庫信息

mysql> show slave status \G;

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

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.253.130

Master_User: administrotar

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000045

Read_Master_Log_Pos: 2575

Relay_Log_File: mysql-relay.000002

Relay_Log_Pos: 1222

Relay_Master_Log_File: mysql-bin.000045

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

Relay_Log_Space: 1374

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

接下來就是測試了!可自己測試

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 這篇博客會詳細(xì)介紹如何配置主從配置,但重點是想分享如何當(dāng)配置未成功時,如何調(diào)試。 原文連接(推薦這個看 比較清晰)...
    海里的沙丁魚閱讀 999評論 0 2
  • 圖1:很久前,只有3只羊,它們在草原上歡快奔跑; 圖2:后來它們有了老婆和孩子,為了晚上睡個安穩(wěn)覺,他們做了個簡單...
  • 我現(xiàn)在臉皮很厚,不怕你拿話戳我??晌乙膊辉趺锤腋愣嗾f,總有臉皮被戳破的那天。
    小北北北閱讀 195評論 0 0
  • 胡凝閱讀 272評論 0 0
  • 畢業(yè)后到處漂泊,終于有天累了,想家了,那就回去吧。有那么一刻真的想在老家找個工作,這一生再也不想過分的苛求什么。想...
    觀棋爛柯閱讀 247評論 0 0

友情鏈接更多精彩內(nèi)容