1、初始准备
1.1、版本一致
1.2、初始化表,并在后台启动mysql
1.3、修改root的密码
2、修改主服务器master
vi /etc/my.cnf
[mysqld] log-bin=mysql-bin #[必须]启用二进制日志 server-id=28 #[必须]服务器唯一ID,默认是1,一般取IP最后一段
3、修改从服务器slave:
vi /etc/my.cnf
[mysqld] log-bin=mysql-bin #[不是必须]启用二进制日志 server-id=29 #[必须]服务器唯一ID,默认是1,一般取IP最后一段
4、重启两台服务器的mysql
/etc/init.d/mysql restart
5、在主服务器上建立帐户并授权slave
mysql –u root -p
GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by '1qaz!QAZ'; Flush privileges;
//一般不用root帐号,‘%’表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如10.1.30.29,加强安全。
(如果是主主复制,可以两边分别都建立)
6、登录主服务器的mysql,查询master的状态
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 252 | | |
+------------------+----------+--------------+---------------
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
7、配置从服务器Slave
mysql>change master to master_host='10.1.30.28',master_user='mysync',master_password='1qaz!QAZ',master_log_file='mysql-bin.000001',master_log_pos=252;
#注意不要断开,252数字前后无单引号。(注意此处写的代码与master上面查询status的结果是相关的,按照实际修改)
Mysql>start slave;
启动从服务器复制功能
8、检查从服务器复制功能状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.30.23
Master_User: mysync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 252
Relay_Log_File: mysqld-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:
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: 252
Relay_Log_Space: 407
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及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
以上操作过程,主从服务器配置完成。
9、主从服务器测试:
主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:
mysql> create database test_db; Query OK, 1 row affected (0.00 sec) mysql> use test_db; Database changed mysql> create table test_tb(id int(3),name char(10)); Query OK, 0 rows affected (0.00 sec) mysql> insert into test_tb values(001,'jack'); Query OK, 1 row affected (0.00 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test_db |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
从服务器Mysql查询:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test_db | //能够看到主服务器创建的数据库
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use test_db
Database changed
mysql> select * from test_tb; //查看主服务器上新增的具体数据
+------+------+
| id | name |
+------+------+
| 1 | jack |
+------+------+
1 row in set (0.00 sec)