mysql主从设定笔记
CREATE TABLE Persons
(
Id int unsigned not null AUTO_INCREMENT,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255),
created datetime,
primary key (id)
)type=MyISAM AUTO_INCREMENT = 1;
INSERT INTO Persons(LastName,FirstName,created) VALUES (’Huaming’,'Yue’,NOW());
//主服务器
[mysqld]
log-bin=mysql-bin
server-id=1
binlog-ignore-db=mysql, test
#binlog-do-db=vbb
//建立账号供从服务器使用
GRANT ALL ON *.* TO rep_slave@’%’ IDENTIFIED BY ‘joeyue’;
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO rep_slave@’%’ IDENTIFIED BY ‘joeyue’;
FLUSH PRIVILEGES;
从服务器
log-bin=mysql-bin
server-id=2
master-host=192.168.1.104
master-user=rep_slave
master-password=joeyue
master-port=3306
master-connect-retry=60
log-slave-updates
#replicate-do-db=foxshare //指定同步的数据库
INSERT INTO Persons(Id,LastName,FirstName,created) VALUES (2,’Huaming’,'Yue’,NOW());
//注意
Share the same database and table structure
<?php
$link=mysql_connect(’192.168.1.104:3306′,’user’,'password’);
if(!$link) {
echo ‘failed’;
} else {
mysql_select_db(’Persons’, $link);
for($i=0; $i<50;$i++) {
mysql_query(”INSERT INTO Persons(Id,LastName,FirstName,created) VALUES (2,’Huaming’,'Yue’,NOW());”);
}
}
mysql_close($link);
?>
下面是具体配置过程:
Master:
1.为从机(Slave)创建一个复制权限账户
mysql > GRANT REPLICATION SLAVE ON *.* TO ’slave’@'172.20.92.110′ IDENTIFIED BY ’slave’;
Ps:replication slave 为单一的复制权限 用户名slave 密码slave
2.锁定主机数据库 导出当前锁定状态下所有数据到从机中 做好复制前的基本数据同步工作
mysql > FLUSH TABLES WITH READ LOCK;
mysql > exit
[root@%%] # mysqldump -uroot -p test > test.sql
3.查询当前主机数据库文件和位置
mysql > show master status;
——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000041 | 1509 | | |
+——————+———-+————–+——————+
Ps:记录下这两个值(文件名、当前位置),一会儿配置从机连接主机时要用到。
Slave:
1.创建数据库 test ,将主机Master生成的sql文件导入到test数据库中。
2.关掉Slave,将主机日志文件和位置信息读入从机
mysql > stop slave;
mysql > change master to
->master_log_file=’mysql-bin.000041′,
->master_log_pos=1509;
mysql > start slave;
mysql > show slave status \G
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.20.92.108 ##主机ip
Master_User: slave ##复制账号
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000041 ##主机日志文件
Read_Master_Log_Pos: 1509 ##主机日志文件位置
Relay_Log_File: mysqld-relay-bin.000007 ##从机中继日志文件
Relay_Log_Pos: 235 ##中继日志文件位置
Relay_Master_Log_File: mysql-bin.000041
Slave_IO_Running: Yes ##从机IO线程 连接主机
Slave_SQL_Running: Yes ##从机sql线程 处理更新的日志
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: 1509
Relay_Log_Space: 235
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
1 row in set (0.00 sec)
接下来,解锁Master,允许主机数据库更新。
Master:
mysql > unlock tables;