步骤如下:
1、主从服务器分别作以下操作:
1.1、版本一致 1.2、初始化表,并在后台启动mysql 1.3、修改root的密码2、修改主服务器master:#vim /etc/my.cnf [mysqld] log-bin=mysql-bin //[必须]启用二进制日志 server-id=222 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
#vi /etc/my.cnf [mysqld] log-bin=mysql-bin //[不是必须]启用二进制日志 server-id=226 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
#service mysqld restart
#/usr/local/mysql/bin/mysql -uroot -p123456 mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456'; //一般不用root帐号,'%';表示所有客户端都可能连,只要帐号,密码正确,此处可 用具体客户端IP代替,如192.168.145.226,加强安全。
mysql>show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 308 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
7、配置从服务器Slave:mysql>change master to master_host='192.168.145.222',master_user='mysync',master_password='q123456', master_log_file='mysql-bin.000004',master_log_pos=308; //注意不要断开,308数字前后无单引号。 Mysql>start slave; //启动从服务器复制功能
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.222 //主服务器地址 Master_User: mysync //授权帐户名,尽量避免使用root Master_Port: 3306 //数据库端口,部分版本没有此行 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 600 //#同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos Relay_Log_File: ddte-relay-bin.000003 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes //此状态必须YES Slave_SQL_Running: Yes //此状态必须YES ......
mysql> create database hi_db; Query OK, 1 row affected (0.00 sec) mysql> use hi_db; Database changed mysql> create table hi_tb(id int(3),name char(10)); Query OK, 0 rows affected (0.00 sec) mysql> insert into hi_tb values(001,'bobu'); Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hi_db | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hi_db | //I'M here,大家看到了吧 | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> use hi_db Database changed mysql> select * from hi_tb; //查看主服务器上新增的具体数据 +------+------+ | id | name | +------+------+ | 1 | bobu | +------+------+ 1 row in set (0.00 sec)
文章引自:http://369369.blog.51cto.com/319630/790921/