mysql5.6一主一从
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql5.6一主一从相关的知识,希望对你有一定的参考价值。
mysql5.6一主一从
借鉴
http://369369.blog.51cto.com/319630/790921/
mysql主从复制
主机规划:
192.168.0.205 edu-mysql-01 master
192.168.0.206 edu-mysql-02 salve
1、主从服务器分别作以下操作:
1.1、版本一致
1.2、初始化表,并在后台启动mysql
1.3、修改root的密码
2、修改主服务器master:
[[email protected] ~]# cat /etc/my.cnf
[mysqld]
user=mysql
pid_file = /var/lib/mysql/mysqld.pid
socket = /var/lib/mysql/mysql.sock
port = 3306
basedir = /usr/local/mysql
datadir = /var/lib/mysql
tmpdir = /tmp
log-bin = master-bin
log-bin-index = master-bin.index
server_id = 1
innodb_log_file_size = 256M
expire-logs-days = 1
3、修改从服务器slave:
[[email protected] ~]# cat /etc/my.cnf
[mysqld]
user=mysql
pid_file = /var/lib/mysql/mysqld.pid
socket = /var/lib/mysql/mysql.sock
port = 3306
basedir = /usr/local/mysql
datadir = /var/lib/mysql
tmpdir = /tmp
server_id = 2
relay_log_index = slave_relay_bin.index
relay_log = slave_relay_bin
innodb_log_file_size = 256M
expire-logs-days = 1
4、重启两台服务器的mysql
/etc/init.d/mysql restart
5、在主服务器上建立帐户并授权slave:
#/usr/local/mysql/bin/mysql -uroot -p123456
mysql>GRANT REPLICATION SLAVE ON *.* to ‘mysync‘@‘%‘ identified by ‘q123456‘; //一般不用root帐号
6、登录主服务器的mysql,查询master的状态
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000009 | 320 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000004‘, MASTER_LOG_POS=431;
change master to master_host=‘192.168.0.32‘,master_user=‘repl‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000004‘,master_log_pos=431;
7、配置从服务器Slave:
mysql>change master to master_host=‘192.168.0.205‘,master_user=‘mysync‘,master_password=‘q123456‘,master_log_file=‘master-bin.000009‘,master_log_pos=320;
Mysql>start slave; //启动从服务器复制功能
8、检查从服务器复制功能状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.205 //主服务器地址
Master_User: mysync //授权帐户名,尽量避免使用root
Master_Port: 3306 //数据库端口,部分版本没有此行
Connect_Retry: 60
Master_Log_File: master-bin.000009
Read_Master_Log_Pos: 760 //#同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos
Relay_Log_File: slave_relay_bin.000002
Relay_Log_Pos: 724
Relay_Master_Log_File: master-bin.000009
Slave_IO_Running: Yes //此状态必须YES
Slave_SQL_Running: Yes //此状态必须YES
......
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
以上操作过程,主从服务器配置完成。
9、主从服务器测试:
主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:
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查询:
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://7038006.blog.51cto.com/7028006/1893609
以上是关于mysql5.6一主一从的主要内容,如果未能解决你的问题,请参考以下文章