mysql 5.6 主从表访问_mysql5.6 搭建主从复制环境

192.168.50.5 (作为 master )

192.168.50.6 (作为 slave )

搭建主从的几个必要条件

主库的 server-id=1323306、从库的 server-id=1323306 保证两者不一致

主库开启 binlog 功能

为了后期不出现数据不一致的情况 保证 binlog 日志格式为 row模式来实施搭建过程

环境说明:

Vagrant + VirtualBox + Centos7

虚拟机需要配置端口转发和文件挂载功能

如果你是root用户 以下所有命令不需要 sudo

config.vm.synced_folder "/Users/apple/Desktop/pkgs", "/home/vagrant/pkg"

#主库

config.vm.network "forwarded_port", guest: 3306, host: 9005

config.vm.network "private_network", ip: "192.168.50.5"

#从库

config.vm.network "forwarded_port", guest: 3306, host: 9006

config.vm.network "private_network", ip: "192.168.50.6"

在主库(192.168.50.5)上的操作

设置 mysql 配置

sudo vim /etc/my.cnf

my.cnf 内容如下

[client]

port=3306

socket = /tmp/mysql.sock

default-character-set = utf8

[mysql]

default-character-set = utf8

[mysqld]

port = 3306

socket = /tmp/mysql.sock

basedir = /usr/local/mysql

datadir = /data/mysql

open_files_limit = 65535

back_log = 103

max_connections = 512

max_connect_errors = 100000

table_open_cache = 512

external-locking = FALSE

max_allowed_packet = 128M

sort_buffer_size = 2M

join_buffer_size = 2M

thread_cache_size = 51

query_cache_size = 32M

tmp_table_size = 96M

max_heap_table_size = 96M

slow_query_log = 1

slow_query_log_file = /data/mysql/slow.log

log-error = /data/mysql/error.log

long_query_time = 0.5

server-id = 1323306

log-bin = /data/mysql/mysql-bin

sync_binlog = 1

binlog_cache_size = 4M

max_binlog_cache_size = 128M

max_binlog_size = 1024M

expire_logs_days = 7

key_buffer_size = 32M

read_buffer_size = 1M

read_rnd_buffer_size = 16M

bulk_insert_buffer_size = 64M

character-set-server = utf8

default-storage-engine = InnoDB

binlog_format = row

#gtid_mode = on

#log_slave_updates = 1

#enforce_gtid_consistency = 1

interactive_timeout = 300

wait_timeout = 300

transaction_isolation = REPEATABLE-READ

innodb_buffer_pool_size = 1434M

innodb_data_file_path = ibdatal:1024M:autoextend

innodb_flush_log_at_trx_commit = 1

innodb_log_buffer_size = 16M

innodb_log_file_size = 256M

innodb_log_files_in_group = 2

innodb_max_dirty_pages_pct = 50

innodb_file_per_table = 1

innodb_locks_unsafe_for_binlog = 0

[mysqldump]

quick

max_allowed_packet = 32M

添加用户和用户组

sudo groupadd mysql

sudo useradd -g mysql mysql -s /sbin/nologin

Mysql 安装到/usr/local目录下 方便管理

将挂载的mysql文件拷贝一份到/usr/local目录下 具体看你文件挂载到哪个目录

cd /usr/local

sudo tar -zxvf mysql-5.6.16-linux-glibc2.5-x86_64.tar.gz

sudo ln -s mysql-5.6.16-linux-glibc2.5-x86_64 mysql

sudo chown mysql:mysql -R mysql

sudo mkdir -p /data/mysql

sudo chown mysql:mysql -R /data/mysql

安装前先解决依赖问题

sudo yum install perl-Data-Dumper -y

sudo yum install -y libaio libaio-devel -y

安装

cd /usr/local/mysql/bin/scripts

sudo ./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --defaults-file=/etc/my.cnf --user=mysql

启动mysql服务

cd /usr/local/mysql/bin/

sudo ./mysqld_safe --defaults-file=/etc/my.cnf &

查看mysql服务是否已经启动

ps -ef | grep mysql

登录mysql 默认没有密码 直接enter键进入 重置密码和删除危险账号并且刷新MySQL的系统权限相关表

./mysql -uroot -p

use mysql;

update user set password=password('root123') where user='root';

flush privileges;

delete from user where user != 'root' or host != 'localhost';

关闭mysql服务

./mysqladmin -uroot -proot123 shutdown

由于要做主从需要开启远程连接 记得刷新MySQL的系统权限相关表

use mysql;

GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY 'root123';

flush privileges;

创建一个主从复制的账号 记得刷新MySQL的系统权限相关表

create user 'bak'@'192.168.50.%' identified by 'bak123';

grant replication slave on *.* to 'bak'@'192.168.50.%';

flush privileges;

初始化数据

--master-data=2 说明 让备份出来的文件记录这一时刻的 binlog 文件和 position 号 为搭建主从环境做准备

我这边是在 /home/vagrant/pkg 目录下执行此命令的 方便从库导入

/usr/local/mysql/bin/mysqldump --single-transaction -uroot -proot123 --master-data=2 -A > all.sql

在从库(192.168.50.6)上的操作

从库 按上面步骤安装一下后导入主库数据库 记得更改server-id 不要和主库一样

恢复主库传递过来的数据

/usr/local/mysql/bin/mysql -uroot -proot123 < all.sql

在数据库命令执行配置主从命令

CHANGE MASTER TO MASTER_HOST='192.168.50.5',MASTER_USER='bak',MASTER_PASSWORD='bak123',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=2396;

执行开始主从复制的命令

start slave;

查看主从复制的状态

当从库 Slave_IO_Running 和 Slave_SQL_Running 都呈现 Yes 状态 代表主从复制开始工作了

show slave status \G;

mysql 5.6 主从表访问_mysql5.6 搭建主从复制环境_第1张图片

你可能感兴趣的:(mysql,5.6,主从表访问)