MySQL安装-主从复制(5)

环境设置
1master 1slave
192.168.1.40 master
192.168.1.41 slave1
安装mysql
Yum 安装方式(2个服务器同时装)
rpm -qpl mysql57-community-release-el7-11.noarch.rpm
rpm -ivh mysql57-community-release-el7-11.noarch.rpm
MySQL安装-主从复制(5)
yum -y install mysql-client mysql-server

systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld
MySQL安装-主从复制(5)
grep ‘password‘ /var/log/mysqld.log |head -n 1
mysql 主从构建
MASTER配置文件设置
Master 配置文件中开启bin-log文件,设置server-id,设置完毕重新启动mysql
[root@mysqlcluster1 ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-bin=mysql-bin
server-id= 1
[mysql_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
MySQL安装-主从复制(5)
server-id:为主服务器A的ID值
log-bin:二进制变更日值
master上添加复制用户
授权同步用户和密码
登录mysql 数据库,新建数据库账号,赋予同步权限
mysql -uroot -p
MySQL安装-主从复制(5)
在主服务器新建一个用户赋予“REPLICATION SLAVE”的权限。你不需要再赋予其它的权限。在下面的命令,把X.X.X.X替换为从服务器的IP。

  1. mysql>CREATE USER ‘user‘@ ‘X.X.X.X‘ IDENTIFIED BY ‘password‘;
  2. mysql>GRANT REPLICATION SLAVE ON . TO ‘user‘@‘X.X.X.X‘ IDENTIFIED BY ‘password‘;
    执行以下命令锁定数据库以防止写入数据。
    mysql>FLUSH TABLES WITH READ LOCK;

mysql> GRANT REPLICATION SLAVE ON . TO ‘tongbu‘@‘%‘ IDENTIFIED BY ‘P@ssw0rd‘;
MySQL安装-主从复制(5)
设置从库slave
Slave执行change master绑定主库
slave配置文件设置
vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-bin=mysql-bin
server-id= 2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
MySQL安装-主从复制(5)
执行change master

  1. mysql>CHANGE MASTER TO
  2. MASTER_HOST=‘X.X.X.X‘,
  3. MASTER_USER=‘user‘,
  4. MASTER_PASSWORD=‘password‘,
  5. MASTER_PORT=3306,
  6. MASTER_LOG_FILE=‘mysql-bin.000001‘,
  7. MASTER_LOG_POS=98,
  8. MASTER_CONNECT_RETRY=10;
    MASTER_HOST:主服务器的IP。
    MASTER_USER:配置主服务器时建立的用户名
    MASTER_PASSWORD:用户密码
    MASTER_PORT:主服务器mysql端口,如果未曾修改,默认即可。

mysql> change master to master_host=‘192.168.1.51‘,
-> master_user=‘tongbu‘,
-> master_password=‘P@ssw0rd‘,
-> master_log_file=‘mysql-bin.000001‘,
-> master_log_pos=444;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
change master to master_host=‘192.168.1.51‘,master_user=‘tongbu‘,master_password=‘P@ssw0rd‘,master_log_file=‘mysql-bin.000002‘,MASTER_LOG_POS=1084;
MySQL安装-主从复制(5)
启动slave服务
mysql> start slave;
MySQL安装-主从复制(5)

测试
在master数据库服务器创建一个数据库文件
mysql> show slave status\G
MySQL安装-主从复制(5)
mysql> create database mysqltesting charset=utf8;
MySQL安装-主从复制(5)
去从数据库查看
mysql> show databases;
MySQL安装-主从复制(5)
查看文件内容
查看bin-log文件
mysql> show master status;
MySQL安装-主从复制(5)
从库是从bin-log 444之后同步
mysql-bin.000001 文件位置在/var/lib/mysql/
ll /var/lib/mysql
MySQL安装-主从复制(5)
bin-log文件内容
[root@mysqlcluster1 mysql]# mysqlbinlog mysql-bin.000002 |more
Bin-log文件中都是一些执行的步骤
记录了时间段及编号段内的执行的内容
MySQL安装-主从复制(5)
relay-log日志
MySQL安装-主从复制(5)
mysqlbinlog mysqlcluster2-relay-bin.000002 |more
MySQL安装-主从复制(5)
master.info文件
Master.info文件在slave机器上,主要记录了同步账号和密码
cat master.info
MySQL安装-主从复制(5)

MySQL安装-主从复制(5)

上一篇:MySQL安装-读写分离(7)


下一篇:MySQL安装-yum安装部署MySQL(2)