安装mysql
# tar zxvf mysql-max-5.0.27-linux-i686-glibc23.tar.gz
# groupadd mysql
# useradd -g mysql mysql
# mv mysql-max-5.0.27-linux-i686-glibc23 mysql
# cd mysql
# chown -R mysql:mysql .
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
# cp support-files/my-medium.cnf /etc/my.cnf
# scripts/mysql_install_db --user=mysql
# chkconfig --add mysqld
# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
主 10.99.99.50
从 10.99.99.52
编辑主配置文件
# vi /etc/my.cnf
在[mysqld]下添加
server-id=1
log-bin
保存退出
#service mysqld restart
# mysql
mysql>grant replication slave on *.* to 'bakup'@'10.99.99.52' identified by '123';
mysql>show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000006 | 98 | zld | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
记下File和Position下的值
编辑从配置文件
# vi /etc/my.cnf
在[mysqld]下添加
server-id=2
master-host=10.99.99.50
master-user=bakup
master-passwor=123
master-port=3306
master-connect-retry=60
replicate-do-db=zld
log-bin
binlog-do-db=zld
保存退出
#service mysqld restart
#mysql
mysql>slave stop
mysql>change master to master_host='10.99.99.50',master_user='bakup',master_pas
sword='123',master_log_file='mysql-bin.000005',master_log_pos=98;
mysql>slave start
mysql>show slave status \G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 10.99.99.50
Master_User: bakup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 98
Relay_Log_File: test-relay-bin.000001
Relay_Log_Pos: 98
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB: zld
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: 98
Relay_Log_Space: 98
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: NULL
1 row in set (0.00 sec)
发现 Slave_IO_Running: No,这表示不成功,应该为yes
查看mysql日志
# cat /usr/local/mysql/data/test.com.err
[ERROR] The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
解决办法:
# vi /etc/my.cnf
在[mysqld]下添加
replicate-same-server-id
保存退出
# service mysqld restart
#mysql
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.99.99.50
Master_User: bakup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 98
Relay_Log_File: test-relay-bin.000003
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: zld
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: 98
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.01 sec)
发现已经变为 Slave_IO_Running: Yes ,表示成功
测试
主
#mysql
mysql> create database zld;
mysql>use zld;
mysql>create table user(id int);
mysql>insert into user values('1');
从
#mysql
mysql>show databases;
mysql>use zld;
mysql>select * from user;
可以看到在主上创建的内容全部都显示在从上
做这个测试的时候注意的问题就是出现问题要看mysql的日志,可以去网上搜索错误的提示获得帮助。