MySql教程

MYSQL 主从复制

本文主要是介绍MYSQL 主从复制,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

考虑到一般的部署场景主备,服务主备、存储主备、网关主备等等,我们先了解一下简单的mysql 主备是怎么做的

mysql主从复制

第一步

分别安装两个mysql,版本5.7.5

1主节点配置:

[mysql]
#设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
log_bin=mysql-bin

#These are commonly set, remove the # and set as required.
basedir = C:\Program Files\mysql5.7.5matser\
datadir = C:\Program Files\mysql5.7.5matser\data
port = 3306
server_id = 1
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#同步的数据库,除此之外别的不同步(和下面binlog-ignore-db二选一)
binlog_do_db=test 
//#不同步数据库,除此之外都同步
#binlog-ignore-db = information_schema 
#binlog-ignore-db = mysql

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

2、mysql 环境配置和服务启动

1、配置环境变量,path:C:\Program Files\mysql5.7.5matser\bin
2、执行mysqld --intsall mysqlMaster(mysql服务名)
3、执行net start mysqlMaster
如果第三方执行失败,请执行mysqld --remove mysqlMaster,在执行mysqld --defaults-file=C:\Program Files\mysql5.7.5matser\my.ini --initialize-insecure
重复执行2、3,如果还是启动失败执行mysqld --console 查看启动日志报错信息做相应的修改
4、登录数据库修改密码
mysql -uroot -p,根据提示设置密码

3、按照同样的部署部署从节点mysql-slave修改配置,主要是端口和文件目录发生变化

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
log_bin=mysql-bin

#These are commonly set, remove the # and set as required.
basedir = C:\Program Files\mysql5.7.5slave\
datadir = C:\Program Files\mysql5.7.5slave\data
port = 3307
> server_id = 2
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

4、在master操作

1、添加主从同步mysql 账号test和密码123456,
2、执行语句grant replication slave,reload,super on . to test@localhost identified by ‘123456';flush privileges;
3、执行语句show master status; 查看bin log文件名(fileName)和位置(pos)

5、登录slave

1、执行同步请求的sql: change master to master_host='ip',master_port=3306,master_user='test',master_password='123456',master_log_file='fileName',master_log_pos=pos;
2、执行start slave,并且查看同步状态 show slave status;,观察Slave_IO_Running和Slave_SQL_Running如果两个不为yes,则异常

6 测试

1、在配置要同步的数据库里边做创建表和添加数据的操作,查看从节点是否有变更

常见问题

1、数据不能同步

1、IO线程和sql_线程状态异常,show slave status命令可以查看详细信息

思考

这样可以热备了吗?

这篇关于MYSQL 主从复制的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!