本文主要记录mysql 主从配置。
经典的原理图
0、环境:
- 采用阿里云ECS服务器,同区同配置,操作系统为ubuntus 14 64位,服务器如下:
服务器A:
内网IP: 10.44.94.219 服务器B: 内网IP: 10.44.94.97- 安装mysql环境,命令如下:
sudo apt-get update
sudo apt-get install mysql-server mysql-client1 Master 配置:- 创建复制账号:
GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'backup'@'10.44.94.97' IDENTIFIED BY 'fredric';
- 拷贝数据(若存在历史数据)
- 开启bin-log
在my.cnf文件[mysqld]标签下增加
log-bin=mysql-bin.log server-id=97 #server-id 建议采用ip地址末尾备注:若binlog目录指定在其他地方,需要修改文件夹权限
配置完重启mysql 服务:sudo /etc/init.d/mysql restart 进入后输入:SHOW MASTER STATUS显示:
+------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 107 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)2 Slave 配置- 开启复制日志
- 配置Slave
CHANGE MASTER TO MASTER_HOST='10.44.94.219',
MASTER_USER='backup', MASTER_PORT=3306, MASTER_PASSWORD='fredric', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=107;- 开启复制
START SLAVE;
输入:SHOW SLAVE STATUS\G; 显示:Slave_IO_State: Waiting for master to send eventMaster_Host: 10.44.94.219Master_User: backupMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000002Read_Master_Log_Pos: 107Relay_Log_File: mysql-relay-bin.000002Relay_Log_Pos: 253Relay_Master_Log_File: mysql-bin.000002Slave_IO_Running: YesSlave_SQL_Running: Yes
测试遇到的问题:
报错:Last_IO_Error: error connecting to masterSLAVE服务器无法通过局域网IP连接MASTER服务器的数据库,原因是mysql默认安装时只绑定127.0.0.1这个IP访问,因此要修改my.cnf注释掉其中的bind-address = 127.0.0.1