运行环境:
虚拟机安装的是CentOS5
Mysql版本:5.1.31- log MySQL Community Server (GPL)
操作步骤:
1. 在主从服务器上分别安装mysql
shell> groupadd mysql
shell> useradd -g mysql mysql
shell> cd /usr/local
shell> gunzip < /path/to/mysql-VERSION-OS.tar.gz | tar xvf -
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
2. 配置主服务器
2.1 先登录主机(59.57.251.56)
mysql>grant all on *.* to root@'%' identified by '1234'; -- 添加权限(赋予从机权限,有多台丛机,就执行多次)
mysql>GRANT All ON *.* TO 'backup'@'%' IDENTIFIED BY 'testbak';
mysql>flush privileges; -- 刷新权限
mysql>select host,user from user; --查看权限
4. 先重启主机A的 mysql,再重启从机B的mysql
If you get this error File '/var/log/mysql/mysql-bin.index' not found (Errcode: 13) you might want to try and chown -R mysql:mysql /var/log/mysql
5. 验证
在主机A中,mysql>show master status\G;
在从机B 中,mysql>show slave status\G;
能看到大致这些内容
File: mysql-bin.000001
Position: 1374
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.139.77
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 106
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
Replicate_Ignore_DB: mysql
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: 106
Relay_Log_Space: 410
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:
1 row in set (0.00 sec)
查看 Slave_IO_State字段空 //复制没有启动
Connecting to master//没有连接上master
Waiting for master to send event// 已经连上
补充: 可以使用LOAD DATA FROM MASTER语句来建立 slave。但有约束条件:
数据表要全部是MyISAM表,必须有SUPER权限,master的复制用户必须具备RELOAD和SUPER权限。
在master端执行RESET MASTER清除已有的日志变更,
此时slave端会因为找不到master日志无法启动 IO_THREAD,请清空data目录下
relay-log.info,hosname-relay-bin*等文件重新启动mysql
中继日志文件默认的文件为hostname-relay-bin.nnn和hostname-relay-bin.index。可用从服务器的--
relay- log和--relay-log-index选项修改。在从服务器中还有一个relay-log.info中继信息文件,可用
--relay- log-info-file启动选项修改文件名。
双机互备则是两个mysql同时配置为master及slave
++主服务器上的相关命令
show master status
show slave hosts
show logs
show binlog events
purge logs to ‘log_name’
purge logs before ‘date’
reset master(老版本flush master)
set sql_log_bin=
++从服务器上的相关命令
slave start
slave stop
SLAVE STOP IO_THREAD //此线程把master段的日志写到本地
SLAVE start IO_THREAD
SLAVE STOP SQL_THREAD //此线程把写到本地的日志应用于数据库
SLAVE start SQL_THREAD
reset slave
SET GLOBAL SQL_SLAVE_SKIP_COUNTER
load data from master
show slave status(SUPER,REPLICATION CLIENT)
CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,MASTER_USER=, MASTER_PASSWORD= // 动态改变master信息
PURGE MASTER [before 'date'] 删除master端已同步过的日志