主从复制-配置

建立基本的主从复制可以总结为以下三个步骤:

配置一个master服务器

配置一个slave服务器

将slave连接至master。

1)、配置master服务器:

要求master服务器开启二进制日志(binary log)和唯一的服务器ID。二进制日志记录了master上的所有变更,并且可以在其他服务器重新执行。服务器ID用于区分服务器。这两部分内容都是需要更改/etc/my.cnf文件,需要停止mysql服务器。

在[mysqld]小节增加如下内容:

log_bin=master-bin#binlog日志文件的基本名

log_bin_index=master-bin-index#binlog日志文件的索引文件名。

expire_logs_days=14#binlog文件保留的天数。0代表永不删除

skip_slave_start#指定复制进程就不会随着数据库的启动而启动

sync_binlog=1#指定binlog文件多久军民一致一次磁盘,值实际为事务提交的次数。

slave_net_timeout=60#设置在多久没收到数据后认为网络超时,之后Slave的IO线程会重新连接Maste

server_id=165

在master创建一个复制用户

mysql> grant replication slave on *.* to"repl_user"@'192.168.10.%' identified by "123456";

2)、配置slave服务器

在slave服务器上,常规情况下我们只需要配置一个唯一的serverID,然后分别使用relay-log和relay-log-index选项配置中继日志文件和中继日志索引文件即可。在slave服务器上停止mysql服务,编辑slave服务器的/etc/my.cnf文件。

在[mysqld]小节增加如下内容:

server-id=166

relay_log=slave-relay-bin

relay_log_index=slave-relay-bin-index

log_slave_updates=1

配置主从服务器的配置后,重新启动两个服务器上的mysqld服务。

3)、同步数据

在正式开启主从复制前,需要保证master与slave服务器上的数据是一致,我们需要在master上备份数据库,然后在slave上恢复数据库,并记录下master上当前相关二进制日志的信息。

mysql> flush tables withread lock;

Query OK, 0 rows affected (0.00sec)

mysql> show master status;

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

| File| Position | Binlog_Do_DB |Binlog_Ignore_DB | Executed_Gtid_Set |

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

| master-bin.000002 |334 ||||

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

1 row in set (0.00 sec)

mysql>

锁定数据库,并记录二进制文件在位置

[root@linux-node1 ~]#mysqldump-uroot –p123456 -A -B –events --default-character-set>/opt/$(date +%F).sql

[root@linux-node1 ~]# ll/opt/2017-03-31.sql

-rw-r--r-- 1 root root 203 Mar31 16:53 /opt/2017-03-31.sql

[root@linux-node1 ~]#

备份所有库,这里使用了mysqldump命令,有很多参数,具体大家可以查阅相关资料,其中有一个参数比较有用,--master-data选项使mysqldump产生change master to语句,值为1时,产生可用的change master to完整语句,为2时,该语句被注释。

mysql> unlock tables;

Query OK, 0 rows affected (0.00sec)

mysql>

解锁主库

[root@linux-node2 ~]# mysql-uroot -p123456

Warning: Using a password onthe command line interface can be insecure.

[root@linux-node2 ~]#

使用主库的备份文件在从库上恢复数据库,这样master与slave上的数据就是相同的了。

4)、slave连接master并启动主从复制

mysql>change master to master_host = '192.168.10.71',master_port = 3306,master_user = 'repl_user',master_password = '123456',master_log_file ='master-bin.000002',master_log_pos = 334;

mysql> start slave;

Query OK, 0 rows affected (0.01sec)

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;

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

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.10.71

Master_User: repl_user

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: master-bin.000002

Read_Master_Log_Pos: 334

Relay_Log_File: slave-relay-bin.000002

Relay_Log_Pos: 284

Relay_Master_Log_File: master-bin.000002

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

Relay_Log_Space: 457

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

Master_UUID:0e9896a7-14f7-11e7-a0e6-000c2900551e

Master_Info_File: /usr/local/mysql-5.6.35/data/master.info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for theslave I/O thread to update it

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

1 row in set (0.00 sec)

ERROR:

No query specified

mysql>

当红色部分出现两个yes之后,说明我们的主从配置正确,并且顺利启动。

我们可以通过使用show

processlist\G命令来查看master及slave上线程的状态:

mysql>show processlist\G;

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

Id: 2

User: root

Host: localhost

db: NULL

Command: Sleep

Time: 2644

State:

Info: NULL

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

Id: 5

User: repl_user

Host: 192.168.10.72:18886

db: NULL

Command: Binlog Dump

Time: 924

State: Master has sent all binlog to slave; waiting for binlog to beupdated

Info: NULL

*************************** 3. row***************************

Id: 6

User: root

Host: localhost

db: NULL

Command: Query

Time: 0

State: init

Info: showprocesslist

3 rows in set (0.00 sec)

ERROR:

No query specified

mysql>

红色部分为处理slave的I/O线程的连接

mysql>show processlist\G;

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

Id: 3

User: system user

Host:

db: NULL

Command: Connect

Time: 846

State: Waiting for master to send event

Info: NULL

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

Id: 4

User: system user

Host:

db: NULL

Command: Connect

Time: 846

State: Slave has read all relay log; waiting for the slave I/O thread toupdate it

Info: NULL

*************************** 3. row***************************

Id: 5

User: root

Host: localhost

db: NULL

Command: Query

Time: 0

State: init

Info: showprocesslist

3 rows in set (0.00 sec)

ERROR:

No query specified

红色部分为I/O线程的状态,蓝色部分为SQL线程的状态

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容