Mysql 主从基本配置
一. MySQL主服务器配置
1、编辑/etc/my.cnf
server-id = 1
log-bin=mysql-bin
# binary logging format - mixed recommended
binlog_format=mixed
binlog-do-db=data2
binlog-ignore-db=mysql
2.建立用户
grant replication slave on *.* to mysql1@192.168.17.23 identified by '1234';
# grant replication slave on *.* to ‘用户名’@'主机’ identified by ‘密码’;
# 在Slave上做连接测试: mysql -h 192.168.17.24 -u mysq1 -p
[root@localhost ~]# /$/mysql -h 192.168.17.24 -u mysql1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
若客户端拒绝连接:
update user set host='%' where user='root';
flush privileges;
grant replication slave on *.* to ‘mysql1’@'192.168.17.23’ identified by ‘1234’;
mysql>flush tables with read lock; 锁表
mysqldump data2 > data2.sql 备份
mysql>unlock tables;
二.MySQL从服务器配置
1.编辑/etc/my.cnf (mysql 5.1.7 以后)
# 如果是win32平台就是编辑my.ini
My.ini代码
[mysqld]
server-id=2 #唯一
#设置要进行或不要进行主从复制的数据库名,同时也要在 Master 上设定。
replicate-do-db=data2
replicate-do-db=数据库名
replicate-ignore-db=mysql
replicate-ignore-db=数据库名
mysql> change master to master_host='192.168.17.24',master_user='mysql1',master_password='1234';
2.编辑/etc/my.cnf (mysql 5.1.7 以前)
# 如果是win32平台就是编辑my.ini
My.ini代码
server-id=2
log-bin=mysql-bin
master-host=192.168.17.24
master-user=mysql1
master-password=1234
master-port=3306
replicate-do-db=data1
master-connect-retry=60
# replicate-do-db=wow 需要备份的数据库名
# replicate-ignore-db=mysql 忽略的数据库(如果需要的话)
# master-connect-retry=60 如果从服务器发现主服务器断掉,重新连接的时间差(秒)
启动从服务器slave线程
mysql>start slave;
执行show processlist命令显示以下进程:
mysql>show processlist\G
在从服务器上执行
mysql> show slave status\G
Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如以上二行同时为Yes 说明配置成功
三、测试:
主数据库中
mysql>use data2;
mysql>create table user1(id int);
mysql>show tables like 'user1'
从数据库中
mysql>use data2;
mysql>show tables;