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
yum -y install mysql-client mysql-server
systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld
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
server-id:为主服务器A的ID值
log-bin:二进制变更日值
master上添加复制用户
授权同步用户和密码
登录mysql 数据库,新建数据库账号,赋予同步权限
mysql -uroot -p
在主服务器新建一个用户赋予“REPLICATION SLAVE”的权限。你不需要再赋予其它的权限。在下面的命令,把X.X.X.X替换为从服务器的IP。
- mysql>CREATE USER ‘user‘@ ‘X.X.X.X‘ IDENTIFIED BY ‘password‘;
- 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‘;
设置从库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
执行change master
- mysql>CHANGE MASTER TO
- MASTER_HOST=‘X.X.X.X‘,
- MASTER_USER=‘user‘,
- MASTER_PASSWORD=‘password‘,
- MASTER_PORT=3306,
- MASTER_LOG_FILE=‘mysql-bin.000001‘,
- MASTER_LOG_POS=98,
- 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;
启动slave服务
mysql> start slave;
测试
在master数据库服务器创建一个数据库文件
mysql> show slave status\G
mysql> create database mysqltesting charset=utf8;
去从数据库查看
mysql> show databases;
查看文件内容
查看bin-log文件
mysql> show master status;
从库是从bin-log 444之后同步
mysql-bin.000001 文件位置在/var/lib/mysql/
ll /var/lib/mysql
bin-log文件内容
[root@mysqlcluster1 mysql]# mysqlbinlog mysql-bin.000002 |more
Bin-log文件中都是一些执行的步骤
记录了时间段及编号段内的执行的内容
relay-log日志
mysqlbinlog mysqlcluster2-relay-bin.000002 |more
master.info文件
Master.info文件在slave机器上,主要记录了同步账号和密码
cat master.info