1, 环境 mysql51:192.168.4.51 master
mysql52:192.168.4.52 slave
192.168.4.50 client
2, 部署上Mysql服务。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1,确保主和从库数据相同,
备份主库,
#mysqldump -uroot -p123456 -B 库名列表 > mytest.sql
恢复到从库,
#scp 192.168.4.51:/root/mytest.sql ./ //scp远程拷贝
把备份数据导入数据库
#mysql -uroot -p123456 < mytest.sql
启用binlog,设置服务器ID,指定日志格式
#vim /etc/my.cnf
[mysqld]
log_bin=master51
server_id=51
binlog_format="mixed" //指出日志存储引擎
# systemctl restart mysqld
#ls /var/lib/mysql/master.*
授权用户
#mysql -uroot -p123456
>grant replication slave on *.* to
repluser@"%" identified by "123qqq...A";
>system mysqlbinlog /var/lib/mysql/master51.000001 | grep -i grant
查看当前使用的日志
>show master status\G; //查看master日志文件状态
设置server_id,
> show slave status;
52]#vim /etc/mysql.cnf
[mysqld]
server_id=52
52]#systemctl restart mysqld
指定主库信息
# mysql -h192.168.4.51 -urepluser -p‘123qqq...A’
> show grants; //查询当前登陆用户的权限
#mysql -uroot -p123456
>change master to
master_host="192.168.4.51", //主库ip
master_user="repluser", //连接用户
master_password="123qqq...A",
master_log_file="master51.000001", //master日志文件,在主库上看
master_log_pos=441; //偏移位置
>start slave; //启动slave进程
>show slave status\G; //看IO和SQL线程都是yes,就成功了
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
报错,排错:
> show slave status\G;
Last_IO_Error: //查看IO线程有报错原因
>stop slave;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4,客户端测试client
51]#mysql -uroot -p123456
> create database db1;
> create table db1.a(id int);
> grant select,insert on db1.* to yaya100@"%" identified by "123qqq...A"; //授权客户端访问的账号
52]#mysql -uroot -p123456
> show databases; //查主创建的库同步过来
> select user from mysql.user where user="yaya100";
//查授权的用户
客户端连接主库写入数据
50 ~]# mysql -h192.168.4.51 -uyaya100 -p123qqq...A
> show grants;
> select * from db1.a;
>desc db1.a;
> insert into db1.a values(100),(200),(201);
> select * from db1.a;
从库也能查到
52 ~]# mysql -uroot -p123456
> select * from db1.a; //看到和50插入相同的数据
=====================================主从配置完成================================================