MySql(18)——Linux MySQL主从配置
MySQL 主從配置
Author:xushuyi
參照技術(shù):http://www.cnblogs.com/kevingrace/p/6256603.html
?
1. 主從數(shù)據(jù)庫(kù)
1、主庫(kù):192.168.56.100
2、從庫(kù):192.168.56.102
3、創(chuàng)建主從數(shù)據(jù)庫(kù)一定要保證主從數(shù)據(jù)庫(kù)字符集編碼的一致性,否則主從同步數(shù)據(jù)會(huì)發(fā)生異常。
因主從數(shù)據(jù)字符集編碼不一致,常見(jiàn)的問(wèn)題如下:
1.?Column 1 of table 'xxx' cannot be converted from type 'varchar(33)' to type 'varchar(11)'
解決方案:http://www.cnblogs.com/wzbz/p/6825761.html
2. 一定要調(diào)整主從數(shù)據(jù)庫(kù)字符集編碼一致性
?
4、創(chuàng)建表時(shí)一定要加上字符集編碼 utf8 設(shè)置,避免主從數(shù)據(jù)同步異常問(wèn)題,其實(shí)遇到問(wèn)題類同 3
1、修改數(shù)據(jù)表字符集:ALTER TABLE transfervouchers CONVERT TO CHARACTER SET utf8;
2、創(chuàng)建表 設(shè)置字符集
create table jpgxhealthvalrecordhistory (ID bigint not null auto_increment comment '主鍵ID',activityid bigint comment '活動(dòng)ID',userid bigint comment '用戶ID',activitylotteryid bigint comment '活動(dòng)抽獎(jiǎng)ID',activityawardid bigint comment '活動(dòng)獎(jiǎng)品ID',contributehealthvalue bigint comment '已貢獻(xiàn)健康值',createtime timestamp comment '創(chuàng)建時(shí)間',modifytime timestamp comment '修改時(shí)間',primary key (ID) )CHARACTER SET utf8; alter table jpgxhealthvalrecordhistory comment '獎(jiǎng)品貢獻(xiàn)健康值歷史記錄表';
?
4、主庫(kù)一旦重啟,主庫(kù)對(duì)應(yīng)二進(jìn)制文件發(fā)生改變,從庫(kù)需要重新指向主庫(kù)新的二進(jìn)制文件
master_host='192.168.56.100',master_user='slave',master_password='Sino@slave12345',? master_log_file='mysql-bin.000002',master_log_pos=400;
?
2.主庫(kù)配置:
#vi /etc/my.cnf
[mysqld]
????log-bin=mysql-bin ???//[必須]啟用二進(jìn)制日志
??? server-id=1100 ??//[必須]服務(wù)器唯一ID,默認(rèn)是1,一般取IP最后一段
binlog-do-db=imove ?//需要同步的數(shù)據(jù)庫(kù)。如果是多個(gè)同步庫(kù),就以此格式另寫幾行即可。如果不指明對(duì)某個(gè)具體庫(kù)同步,就去掉此行,表示同步所有庫(kù)(除了ignore忽略的庫(kù))
binlog-ignore-db=sys ?//#不同步mysql系統(tǒng)數(shù)據(jù)庫(kù)。如果是多個(gè)不同步庫(kù),就以此格式另寫幾行;也可以在一行,中間逗號(hào)隔開(kāi)
?
?
登錄主庫(kù)MySQL
#cd /usr/local/mysql
#./bin/mysql -u root -p
?
?
mysql> create user slave identified by 'Sino@slave12345';
mysql> grant all on *.* to slave@'192.168.56.102' identified by 'Sino@slave12345';
mysql> exit;
?
重啟主庫(kù)MySQL:
#/etc/init.d/mysql restart;
#show master status;
?
?
3.從庫(kù)配置:
vi /etc/my.cnf
log-bin=mysql-bin ?????//[必須]啟用二進(jìn)制日志
server-id=102 ?????//[必須]服務(wù)器唯一ID,默認(rèn)是1,一般取IP最后一段
replicate-do-db=imove //需要同步的數(shù)據(jù)庫(kù)名。如果不指明同步哪些庫(kù),就去掉這行,表示所有庫(kù)的同步(除了ignore忽略的庫(kù))
replicate-ignore-db=sys ?//不同步mysql系統(tǒng)數(shù)據(jù)庫(kù)
slave-skip-errors=all ????//跳過(guò)所有的錯(cuò)誤錯(cuò)誤,繼續(xù)執(zhí)行復(fù)制操作; 這個(gè)配置最好不要加上,遇到問(wèn)題就要解決問(wèn)題,不能直接跳過(guò)
?
?
?
重啟主庫(kù)MySQL:
#/etc/init.d/mysql restart;
之后進(jìn)入從庫(kù)MySQL 修改master配置:
登錄主庫(kù)MySQL
#cd /usr/local/mysql
#./bin/mysql -u root -p
mysql> change master to master_host='192.168.56.100',master_user='slave',master_password='Sino@slave12345', ?????master_log_file='mysql-bin.000002',master_log_pos=400;
查看從庫(kù)master是否配置成功:
?mysql> show slave status\G;
?
======------------------------------可能使用的命令:在更改 master 的時(shí)候------------============
STOP SLAVE IO_THREAD FOR CHANNEL '';
START SLAVE IO_THREAD FOR CHANNEL '';
?
?
?
上圖可以看到: Slave_IO_running: NO
Salve_SQL_Running: NO
需要對(duì)slave mysql 進(jìn)行設(shè)置:
先關(guān)閉slave
mysql> stop slave
之后進(jìn)行設(shè)置:
mysql> set global sql_slave_skip_counter =1 ;
之后再開(kāi)啟:
mysql> start slave
mysql> show slave status\G
?
?
OK.
?
異常情況:
?
?
這個(gè)異常情況說(shuō)明,從庫(kù)同步主庫(kù)二進(jìn)制文件的位置不正確。
解決:http://www.linuxidc.com/Linux/2012-02/54729.htm
將binlog二進(jìn)制文件轉(zhuǎn)換為txt文件
?
?
說(shuō)明從庫(kù)同步主庫(kù)的二進(jìn)制文件應(yīng)該從 123 行開(kāi)始。
登錄從庫(kù)MySQL:
mysql> stop slave;
mysql> change master to master_log_file='mysql-bin.000002',master_log_pos=123;
mysql> start slave;
mysql> show slave status\G;
?
?
4.驗(yàn)證主從同步
之后登錄 master mysql, 進(jìn)行如下操作:
create database hi_db;
use hi_db;
create table hi_tb(id int(3),name char(10));
insert into hi_tb values(001,'bobu');
show databases;
?
?
?
之后登錄slave mysql:
show databases;
可以看到:
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/xushuyi/articles/7699024.html
總結(jié)
以上是生活随笔為你收集整理的MySql(18)——Linux MySQL主从配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 建行LINE FRIENDS信用卡纯爱版
- 下一篇: 随便玩玩之PostgreSQL(第一章)