mysql主从同步

master

vim /etc/my.cnf

[mysqld]

log-bin=binlog

log-bin-index=binlog.index

server_id = 1    确定谁是主谁是从主的版本号小

slave

vim /etc/my.cnf

[mysqld]

server_id = 2

relay_log = /var/lib/mysql/mysql-relay-bin

relay_log_index=/var/lib/mysql/mysql-relay-bin.index


master slave 重启

service mysql restart

关闭防火墙和selinux

service iptable stop

setenforce 0

show variables like 'server_id'; 查看masterslave上的serverID

master上给slave添加一个用户

GRANT ALL PRIVILEGES ON *.* TO ‘zkx’@’%’IDENTIFIED BY ‘123’;

flush privileges;


如果slave不是新库

Cd /var/lib/mysql

rm �Crf *

service mysql restart


master

mysqldump �CA �Cx > /tmp/full.sql      完全备份

scp /tmp/full.sql root@slaveIP:/tmp/


slave

mysql < /tmp/full.sql


master

flush tables with read lock; 锁表

show master status\G

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

| File          | Position | Binlog_Do_DB |Binlog_Ignore_DB | Executed_Gtid_Set |

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

| binlog.000003 |      120 |              |                  |                   |

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

1 row in set (0.00 sec)


unlock tables; 解锁


slave

stop slave

change master to master_host='192.168.1.11', master_port=3306, master_user='zkx', master_password='123', master_log_file='binlog.000006', master_log_pos=120;

start slave

show slave status\G

Slave_IO_Running: yes

Slave_SQL_Running: yes



/usr/local/mysql/data/master.info 记录了主用户名密码等信息如果要重新做主从这个文件必须删除


本文出自 “向前进,向前进” 博客,转载请与作者联系!

你可能感兴趣的:(mysql,防火墙,service,用户,master)