1)配置mysql版本,操作系统
1、mysql版本一致;
2、启动mysql并修改root密码(自己能记住就行)
2)修改主服务器master:
#vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=98 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
3)修改从服务器slave:
#vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[不是必须]启用二进制日志
server-id=115 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
4)重启两台服务器的mysql
service mysql restart
5)在主服务器上建立帐户并授权slave:
###创建一个数据传输账号,自己测试可以直接用root账号。
6)登录主服务器的mysql,查询master的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
7)配置从服务器Slave:
mysql> change master to master_host='192.168.123.115',master_user='root',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=120;
Mysql>start slave; //启动从服务器复制功能
8)检查从服务器复制功能状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.123.115
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 120
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)
9)主从服务器测试:
mysql> create database test_db;
mysql> use test_db;
mysql> create table test_db(id int(3),name char(10));
mysql> insert into test_db values(001,'testName');
mysql> show databases;
从服务器Mysql查询:
mysql> show databases;