【备忘】mysql主从设置

主(master)192.168.1.10机器设置:

[root@vm-vagrant mysql]# vi my.cnf

[mysqld]节点下添加以下配置
server-id=1
log-bin=mysql-bin #这个一定得设置,否则没有日志的话,从数据库上会报错

[root@vm-vagrant mysql]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@vm-vagrant mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.28-log Source distribution

Copyright (c) 2000, 2015, 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> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO backup@'192.168.1.11' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.03 sec)

mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 346 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

从(master)192.168.1.11机器设置:
[root@vm-vagrant mysql]# vi my.cnf

[mysqld]节点下添加以下配置
log-bin = mysql-bin
server_id = 2

重启
[root@vm-vagrant mysql]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!

配置master信息
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.10', MASTER_USER='backup', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=106,MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected, 2 warnings (0.04 sec)

mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

可以查看slave的状态
mysql> show slave status;

发现报错了:
Got fatal error 1236 from master when reading data from binary log: 'log event entry exceeded max_allowed_packet; Increase max_allowed_packet on master; the first event 'mysql-bin.000001' at 106, the last event read from './mysql-bin.000001' at 106, the last byte read from './mysql-bin.000001' at 125.'

将master机上的日志导出来
mysqlbinlog mysql-bin.000001 > test.txt

找到at的最后位置,然后修改上面的chang master语句 中的MASTER_LOG_POS=的值

mysql> change master to master_log_file='mysql-bin.000001',master_log_pos=1157;

然后发现简单主从配置已经实现(配置后续优化)

中间遇到的一个问题:
由于我是在vagrant上的环境,用的同一个.box文件,所以主从复制时造成uuid错误
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
mysql版本如下
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.6.28, for Linux (x86_64) using EditLine wrapper
这种情况只需要将mysql的data目录下的auto.cnf中server-uuid值改成master和slave互相不一致即可
[auto]
server-uuid=89009021-fadf-11e5-b16d-0800274fb806

【附上yii2中使用方法,只需修改配置文件即可】

<?php
return [
'components' => [
/*
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=172.16.10.242;dbname=youban_2.1.3',
'username' => 'root',
'password' => '123456',
'enableSchemaCache' => false, //表结构缓存 正式环境启用
'schemaCache' => 'cache',
'charset' => 'utf8',
],
*/
'db' => [
'class' => 'yii\db\Connection', // 主库的配置
'dsn' => 'mysql:host=192.168.1.10;dbname=youban_2.1.3',
'username' => 'root',
'password' => '123456', // 从库的通用配置
'slaveConfig' => [
'username' => 'root',
'password' => '123456',
'attributes' => [
// 使用一个更小的连接超时
PDO::ATTR_TIMEOUT => 10,
],
], // 从库的配置列表
'slaves' => [
['dsn' => 'mysql:host=192.168.1.11;dbname=youban_2.1.3'],
],
],
]
上一篇:那么都数据库表,那么多不同记录。是怎样都存储在一个key-value数据库的?


下一篇:避免因为Arcgis Server服务设置不当导致Oracle Process溢出的方法