MySQL 5.6 的MASTER – SLAVE 主从实例配置及切换 (一)

来源:互联网 发布:软件流程图模板 编辑:程序博客网 时间:2024/05/20 00:51


MySQL 5.6 MASTER– SLAVE主从实例配置及切换 (一)


1. 任务目标

近期在研究openstack中的trove,涉及到mysql 的 主从热备的部署和切换,

由于流程复杂,为了更好的理解,直接在Linux上部署,测试一下。


2. 参考

a)MySQL5.6 数据库主从(Master/Slave)同步安装与配置详解2016/5/6

http://m.blog.csdn.net/article/details?id=51331244


b)Windows下搭建MySQLMaster Slave 2013-08-09

http://www.cnblogs.com/gaizai/p/3248207.html


c)SHOWSLAVE STATUS 详解2012-02-16

http://blog.csdn.net/shiqidide/article/details/7263652



3.测试环境

机器1: 10.0.0.213


机器210.0.0.195


需要注意是防火墙


使用的linux变种是AmazonLinux AMI

安装mysql

yuminstall -y mysql56-server mysql


关闭防火墙:

serviceiptables stop


这个变种版本没有配置Selinux


检测两台机器间能否ping通。



4.配置MYSQL

Linux环境下MySQL的配置文件的位置是在/etc/my.cnf

创建数据库

createdatabase userdb;

4.I配置MASTER


MYSQLroot用户的密码初始为空,需要更改为123456,命令参考如下

[root@ip-10-0-0-213etc]# mysqladmin -u root -p password 123456

Enterpassword:

Warning:Using a password on the command line interface can be insecure.

[root@ip-10-0-0-213etc]#


配置文件内容如下

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-linksis recommended to prevent assorted security risks

symbolic-links=0

# Settings user and groupare ignored when systemd is used.

# If you need to run mysqldunder a different user or group,

# customize your systemdunit file for mysqld according to the

# instructions inhttp://fedoraproject.org/wiki/Systemd

log-bin=mysql-bin

server-id=213

binlog-ignore-db=information_schema

binlog-ignore-db=cluster

binlog-ignore-db=mysql

# 指定需要同步的数据库

binlog-do-db=userdb

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid




重启服务servicemysqld restart


赋予从库权限帐号

mysql-u root -p123456

mysql>

mysql>GRANT FILE ON *.* TO 'root'@'10.0.0.195' IDENTIFIED BY 'mysqlpassword';

QueryOK, 0 rows affected (0.00 sec)

mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'10.0.0.195' IDENTIFIED BY'mysql password';

QueryOK, 0 rows affected (0.00 sec)

mysql>FLUSH PRIVILEGES;

QueryOK, 0 rows affected (0.00 sec)

mysql>


重启服务,查看主库信息,参考如下:

mysql>show master status;

+------------------+----------+--------------+----------------------------------+-------------------+

|File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+----------------------------------+-------------------+

|mysql-bin.000002 | 120 | userdb |information_schema,cluster,mysql | |

+------------------+----------+--------------+----------------------------------+-------------------+

1row in set (0.00 sec)

mysql>


4.II配置SLAVER

配置文件内容

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

#Disabling symbolic-links is recommended to prevent assortedsecurity risks

symbolic-links=0

#Settings user and group are ignored when systemd is used.

# Ifyou need to run mysqld under a different user or group,

#customize your systemd unit file for mysqld according to the

#instructions in http://fedoraproject.org/wiki/Systemd

log-bin=mysql-bin

server-id=195

binlog-ignore-db=information_schema

binlog-ignore-db=cluster

binlog-ignore-db=mysql

#指定需要同步的数据库

replicate-do-db=userdb

replicate-ignore-db=mysql

log-slave-updates

slave-skip-errors=all

slave-net-timeout=60

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid


重启服务servicemysqld restart


修改对应的MASTER信息

mysql>show slave status;

Emptyset (0.00 sec)

mysql>

mysql>stop slave;##关闭Slave

QueryOK, 0 rows affected, 1 warning (0.00 sec)

mysql>change master tomaster_host='10.0.0.213',master_user='root',master_password='mysqlpassword',master_log_file='mysql-bin.000004',master_log_pos=120;##粗体蓝色部分,对应Master输出信息

QueryOK, 0 rows affected, 2 warnings (0.04 sec)

mysql>start slave;##开启Slave

QueryOK, 0 rows affected (0.01 sec)


查看从库信息,输出参考如下

mysql> show slave status\G;

***************************1. row ***************************

Slave_IO_State:Waiting for master to send event

Master_Host:10.0.0.213

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:userdb

Replicate_Ignore_DB:mysql

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno:0

Last_Error:

Skip_Counter:0

Exec_Master_Log_Pos:120

Relay_Log_Space:457

Until_Condition:None

Until_Log_File:

Until_Log_Pos:0

Master_SSL_Allowed:No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master:0

Master_SSL_Verify_Server_Cert:No

Last_IO_Errno:0

Last_IO_Error:

Last_SQL_Errno:0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id:213

Master_UUID:767e164f-0932-11e7-942a-0e8f186b32da

Master_Info_File:/var/lib/mysql/master.info

SQL_Delay:0

SQL_Remaining_Delay:NULL

Slave_SQL_Running_State:Slave has read all relay log; waiting for the slave I/O thread toupdate it

Master_Retry_Count:86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position:0

1 row in set (0.00 sec)

ERROR:

No query specified

mysql>



5. 测试


5.I MASTER

mysql> use userdb;

Database changed

mysql> show tables;

Empty set (0.00 sec)

mysql> create tabletest1( id int, name char(20) );

Query OK, 0 rows affected(0.27 sec)

mysql> insert into test1( id, name ) values ( 1, 'unknown');

Query OK, 1 row affected(0.02 sec)

mysql>



5.II SLAVE

mysql> use userdb;

Reading table informationfor completion of table and column names

You can turn off thisfeature to get a quicker startup with -A

Database changed

mysql> show tables;

+------------------+

| Tables_in_userdb |

+------------------+

| test1 |

+------------------+

1 row in set (0.00 sec)


mysql>

mysql> show columns from test1;

+-------+----------+------+-----+---------+-------+

| Field | Type | Null |Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| id | int(11) | YES | | NULL | |

| name | char(20) | YES | | NULL | |

+-------+----------+------+-----+---------+-------+

2 rows in set (0.00 sec)

mysql> select * fromtest1;

Empty set (0.00 sec)

mysql> select * fromtest1;

+------+---------+

| id | name |

+------+---------+

| 1 | unknown |

+------+---------+

1 row in set (0.00 sec)

mysql>





进一步

a) 没有测试,如果SLAVE中没有创建对应的数据库userdb的情况下,能够自动创建?


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 12306取消三次后怎么办 外国人护照过期了怎么办 行李丢飞机上怎么办 学生票没次数了怎么办 去美国行李超重怎么办 12306密码找回失败怎么办 12306注册身份重复怎么办 12306身份信息重复怎么办 必修课没有选上怎么办 大学错过了选课怎么办 大学忘记选课了怎么办 火车网上购票儿童票怎么办 售票厅看见小偷怎么办 没赶上火车火车票怎么办 重庆到韩国签证怎么办 重庆去韩国签证怎么办 重庆办韩国签证怎么办 故宫网上预定后怎么办 坐游轮如果晕船怎么办 听听力反应慢怎么办? 毕业证照片丢了怎么办 离线网盘有违规内容怎么办 手机不能向下拉怎么办 cad运行不了插件怎么办 ai中缺少文字怎么办 电脑被格式化了怎么办 迷你世界地图下载失败怎么办 网页上广告太多怎么办 网页打开广告太多怎么办 PS界面图标小怎么办 百度地图反应慢怎么办 汽车导航不播报怎么办 wps菜单栏隐藏了怎么办 手机导航声音小怎么办 手机导航不好用怎么办 手机导航箭头不准怎么办 手机处于离线状态怎么办 穷人让人看不起怎么办 非洲人口过多怎么办啊 鼻子上长白头怎么办 高铁查到违禁品怎么办