mysql5.6配置主从复制;

http://yangsui.me/post/mysql/mysql5.6zhu-cong-fu-zhi-pei-zhi

mysql5.6主从复制配置

于 2015 年 01 月 16 日
一、网络环境

查看mysql版本

masterslave
Server version: 5.5.40-0ubuntu0.14.04.1-log (Ubuntu) Server version: 5.6.19-log Homebrew

网络环境

机器ip地址
master 192.168.1.105
slave 192.168.0.101
二、检查mysq环境

从master备份到slave,需要确保

  • 网络是联通的

    网络是否联通很好确认

  • 防火墙要关闭

    我的master是ubuntu server, 关闭防火墙很简单

    sudo ufw disable

  • 能够连接上mysql, 检查端口以及连接授权

    现在可以测试是否可以远程连接mysql了.

    我从slave执行如下命令

    mysql -uroot -p111111 -h192.168.1.105 -P3306
    
    Warning: Using a password on the command line interface can be insecure.
    ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.105' (51)

    很明显slave(192.168.0.101)无法连接上master(192.168.1.105)上的mysql

    我们看到报的错误是2003, 查阅oracle的官方wiki,简直是体贴入微.一个一个可能去查看.

    原来是MySQL Server accepts clients only on same host这个原因,那就简单了,配置下这个选项就OK.

    sudo vim etc/mysql/my.cnf
    
    #注释掉这行
    #bind-address='127.0.0.1'
  • mysql连接权限设置

    现在可以从其它机器连接到master了,设置下

    grant replication slave on *.* to root@192.168.0.101 identified by '111111' 
    
    #暴力点
    grant all privileges on *.* to root@192.168.0.101 identified by '111111'
    
    #or, 不过当然不建议这么搞
    grant all privileges on *.* to root@'%' identified by '111111'

    让权限生效

    flush privileges
三、基本配置

主要的配置参数

参数解释
server-id 该变量必须在复制拓扑的所有服务器之间唯一,并且使用一个1到4294967296之间的正整数表示
log_bin 服务器必须启用二进制日志才能作为复制的主服务器。如果想要在将来某种情况下(例如发生故障转移或者正常切换时)使得从服务器担任主服务器的角色,从服务器也需要配置二进制日志。使用全局事务ID时也必须在从服务器上启动二进制日志。
log_bin_index 配置的是二进制日志记录文件的目录。该文件中每一行都是二进制日志文件的路径
relay-log 用以记录收到的中继二进制日志
relay-log-index 用以保存收到的日志路径索引
sync-master-info 设置为1以确保不会丢失信息

注意到,有的mysql发行版的参数是log-bin,有的是log_bin,下划线和中横线的区别. 在某一个发行版中统一就OK.

1) master的配置
sudo vim /etc/mysql/my.cnf

server-id               = 1
log_bin                 = /var/log/mysql/mysql-bin
log_bin_index           = /var/log/mysql/mysql-bin.index

#binlog_do_db            = yum # 要复制的数据库
#binlog_ignore_db        = mysql
#binlog_ignore_db        = performance_schema
#binlog_ignore_db        = information_schema
2) slave的配置
sudo vim /usr/local/Cellar/mysql/5.6.19/my.cnf

log-bin=mysql-bin #启用日志
server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin

#sync_master_info = 1
#sync_relay_log = 1
#sync_relay_log_info = 1
3) 为了使配置生效, 重启下master和slave的服务
sudo service mysql restart
四、启动slave与master的连接
1) 重启成功后slave上启动slave与master的连接
#登录mysql
mysql -uroot -p11111

#slave启动于master的连接
mysql> change master to 
    -> master_host='192.168.1.105', #逗号不要漏掉
    -> master_port=3306,
    -> master_user='root',
    -> master_password='111111';

这里要注意:
当我们已经有数据日志文件(bin_log)的时候,我们也可以指定需要同步的文件

mysql> change master to 
    -> master_host='192.168.1.105', #逗号不要漏掉
    -> master_port=3306,
    -> master_user='root',
    -> master_password='111111',
    -> master_log_file='mysql-bin.00006',
    -> master_log_pos='1369';

有关master_log_file master_log_pos参数,参考四章节的2)节 show master status, 以及五章节的数据库准备

#启动slave
mysql> start slave;
2)查看master和slave的状态
#查看slave的状态
mysql> show slave status\G

查看Slave_IO_RunningSlave_SQL_Running选项, 都为YES就可以了

Slave_IO_Running: Connecting
Slave_SQL_Running: Yes

我这里明显有问题,查看Last_IO_ErrnoLast_IO_Error选项

Last_IO_Errno: 2003
Last_IO_Error: error connecting to master 'root@192.168.1.105:3306' - retry-time: 60  retries: 1

2003错误,修改方法见第二点

#查看master的状态
mysql> show master status;

mysql master status

五、测试主从复制功能
1) 新建库测试

master上新建一个数据库:

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

然后在slave上查询:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| yum                |
+--------------------+
5 rows in set (0.00 sec)

看到test数据库已经创建成

2) 同步已有数据库,同步master和slave的表结构

准备数据库和表结构, 保证master和slave数据库同名,创建同样结构的数据库.

#先进入主库,进行锁表,防止数据写入
mysql> flush tables with read lock;

#备份master的数据库
mysqldump -uroot -h192.168.1.105 -p111111 yum>~/sql/192-168-1-105-master-yum.sql

#从备份文件创建同样的数据库
mysql> use yum; 
mysql> source /Users/yangsui/sql/192-168-1-105-master-yum.sql

#移除表锁定
mysql> unlock tables
六、更多的参考文档

最好的当然还是官方文档: replication configuration

csdn 沉沦的介绍: MySQL5.5 主从复制

超详细的介绍: mysql5.6复制介绍

csdn seteor的配置: Mysql5.6主从复制-基于binlog

 

http://zhuxiaoyuan.net/?p=132

MySQL5.6之前版本,安装后,root用户的默认密码是空,到MySQL5.6,安全性增强了,RPM包方式安装后,root用户+空密码不能登陆MySQL了,报如下错误:

[root@bogon MySQL5.6.19]# mysql -uroot 
ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)

那怎么登陆MySQL呢? 通过查找发现,安装的默认密码保存在安装用户Home目录下.mysql_secret文件中,文件内容如下:

[root@bogon ~]# cat .mysql_secret 
# The random password set for the root user at Mon Jan 26 07:55:30 2015 (local time): MmTZsJZZh0k2dvOR

使用给的密码登陆,能登陆成功:

[root@bogon ~]# mysql -uroot -pMmTZsJZZh0k2dvOR 
Warning: Using a password on the command line interface can be insecure. 
Welcome to the MySQL monitor.  Commands end with ; or \g. 
Your MySQL connection id is 5 
Server version: 5.6.19

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its 
affiliates. Other names may be trademarks of their respective 
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>

但是执行show databases命令报错:

mysql> show databases; 
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement

从报错来看,需要重置密码,以下执行重置密码:

mysql> SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘zxy’); 
Query OK, 0 rows affected (0.04 sec)

重新登陆,执行show databases;

[root@bogon ~]# mysql -uroot -pzxy 
Warning: Using a password on the command line interface can be insecure. 
Welcome to the MySQL monitor.  Commands end with ; or \g. 
Your MySQL connection id is 7 
Server version: 5.6.19 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its 
affiliates. Other names may be trademarks of their respective 
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> show databases; 
+——————–+ 
| Database           | 
+——————–+ 
| information_schema | 
| mysql              | 
| performance_schema | 
| test               | 
+——————–+ 
4 rows in set (0.05 sec)

mysql>

 

可以看出,到MySQL5.6版本后,在默认安装后,不能使用空密码登陆了,并且需要强制修改密码,不然不能做其他操作,增强了系统的安全性;

 

 

 

 

 

posted @ 2015-01-28 17:57  陳聽溪  阅读(320)  评论(0)    收藏  举报