欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

mysql主从同步配置(超详细)_MySQL

程序员文章站 2024-01-15 16:56:34
...
bitsCN.com

0.首先在准备两个机器A,B,在上面安装相同版本的mysql(最好是5.0以上)
A为主数据库,B为从数据库
1 mysql同步server-id 格式说明

A B C
A:1:表示域名配置
2:表示SDNS服务器
3:表示探测中心服务器
4:表示收集中心服务器
5:表示探测运算中心服务器
6:表示探测节点服务器
一般
功能模块ID小的为master服务器
功能模块ID大的为slave服务器
B: 一般在同一个局域网内为一组
C:同一功能模块分组内的服务器ID在同一分组内同一个功能模块各个的ID号
例如:3 03 01


2 主从同步配置(主)
1) 修改my.cnf

在my.cnf 中[mysqld] 字段中添加
vi /etc/my.cnf
server-id=10101
log-bin=mysql-bin #这个一定得设置,否则没有日志的话,从数据库上会报错
binlog-ignore-db=mysql,test
innodb_flush_log_at_trx_commit=1
sync_binlog=1
log-slave-updates
2) 重启mysql
service mysql restart
3) 添加帐户给主服务器上mysql的从机用
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'2.2.2.%' IDENTIFIED BY 'testpass';

4) 创建数据库
>create database gslb;
>create database dns;
5) 显示日志位置
>SHOW MASTER STATUS;
+------------------+-----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000003 | 107 | | |
+------------------+-----------+--------------+------------------+

3 主从同步配置(从)
1) 修改my.cnf

在my.cnf 中[mysqld] 字段中添加
vi /etc/my.cnf
server-id = 20101
replicate-do-db = dns
replicate-do-table = gslb.zone
replicate-do-table = gslb.zone_rr
master-info-file = master.info
relay-log = relay-relay-bin
relay-log-index = relay-relay-bin.index
relay-log-info-file=relay-relay-log.info
2) 重启mysql
service mysql restart

3) 设置主服务器信息
change master to master_host='1.1.1.1',
master_user='repl',
master_password='testpass',
master_log_file='mysql-bin.000003',
master_log_pos=107;
4) 启动同步
>start slave;

5) 显示日志位置,出现两个Yes就表示同步成功
mysql> show slave status /G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 1.1.1.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 270500590
Relay_Log_File: relay-relay-bin.000012
Relay_Log_Pos: 1148354
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table: gslb.server_threshold,gslb.server,gslb.threshold,gslb.vip,gslb.status,gslb.vip_threshold
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 270500590
Relay_Log_Space: 1148510
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: 10201
1 row in set (0.00 sec)


6) 导入master的数据
mysql –e “create database gslb”
mysql gslb mysql –e ”create database dns”
mysql dns 本文出自 “一方有” 博客

bitsCN.com