本文主要参考:
并对文中错误进行更正其它参考内容:推荐文档:服务器ip
DB1 10.10.6.211DB2 10.10.6.212
1、主、从服务器安装mysql
~]# yum install mysql mysql-server
2、修改主服务器配置:
[root@DB1 ~]# more /etc/my.cnf 增加如下内容 [mysqld] log-bin=mysql-bin \\[必须]启用二进制日志 server-id=211 \\必须]服务器唯一ID,默认是1,一般取IP最后一段
3、修改从服务器配置:
[root@DB2 ~]# more /etc/my.cnf 增加如下内容[mysqld]log-bin=mysql-bin \\[必须]启用二进制日志server-id=212 \\必须]服务器唯一ID,默认是1,一般取IP最后一段
4、重启两台服务器的mysql
[root@DB1 ~]# service mysqld restart
5、在主服务器上建立帐户并授权给从服务器:
[root@DB1 ~]# mysqlmysql> GRANT REPLICATION SLAVE ON *.* to 'mysync'@'10.10.6.212' identified by '123456';Query OK, 0 rows affected (0.00 sec)mysql>
6、登录主服务器的mysql,查询master的状态
mysql> show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000003 | 260 | | |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)\\注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化mysql>
7、配置从服务器Slave:
注意下面的语句,网上有很多资料都有错误mysql> change master to Master_host='10.10.6.211',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=260;Query OK, 0 rows affected (0.01 sec)\\replicate-ignore-db=mysql //不需要备份的数据库; 这两个语句可以根据实际情况\\replicate-do-db=data //需要备份的数据库 mysql> start slave; \\启动从服务器复制功能Query OK, 0 rows affected (0.00 sec)mysql>
mysql> show slave status\G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.10.6.211 \\主服务器地址 Master_User: mysync \\授权帐户名,尽量避免使用root Master_Port: 3306 \\数据库端口,部分版本没有此行 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 260 \\同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes \\此状态必须YES Slave_SQL_Running: Yes \\此状态必须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: 260 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: 0Master_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)mysql>
以上操作过程,主从服务器配置完成。
9、主从服务器测试:
主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:
mysql> create database jedy_db; Query OK, 1 row affected (0.00 sec)mysql> use jedy_db;Database changedmysql> create table jedy_test(id int(3),name char(10));Query OK, 0 rows affected (0.00 sec)mysql> insert into jedy_test values(001,'tian');Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || jedy_db || mysql || test |+--------------------+4 rows in set (0.00 sec)mysql>
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || jedy_db || mysql || test |+--------------------+4 rows in set (0.00 sec)mysql> mysql> use jedy_db;Database changedmysql> show tables;+-------------------+| Tables_in_jedy_db |+-------------------+| jedy_test | \\主服务器上新建的库传过来了+-------------------+1 row in set (0.00 sec)mysql> select * form jedy_test; \\可以看到在主服务器上新增的具体数据ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'form jedy_test' at line 1mysql> select * from jedy_test; +------+------+| id | name |+------+------+| 1 | tian |+------+------+1 row in set (0.00 sec)mysql>