系统:redhat6.5
防火墙:保持关闭
selinux=disabled
mysql主机:server1 172.25.60.1/24
mysql从机:server2 172.25.60.2/24
(1)安装包
mysql-community-client-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-server-5.7.17-1.el6.x86_64.rpm
(2)开启MySQL并修改密码
##开启mysql
[root@server3 ~]# /etc/init.d/mysqld start
##查看密码
grep password /var/log/mysqld.log
##修改mysql的管理用户密码
[root@server3 ~]# mysql -p
Enter password: ##输入查看到的密码
mysql> ALTER USER root@localhost identified by 'newpassword';
(1)配置主配置文件(/etc/my.cnf)
[root@server1 ~]# vim /etc/my.cnf
server-id=1 ##服务器ID
log-bin=mysql-bin ##开启二进制日志
binlog-do-db=test ##需要同步的数据库名
binlog-ignore-db=mysql ##禁止同步的数据库名
[root@server1 ~]# /etc/init.d/mysqld restart ##重启服务
(2)配置数据库
##在主库上建立帐户并授权
##为了方便我将密码全部设置一致
mysql> grant replication slave on *.* to 'server2'@'172.25.60.2' identified by '授权密码';
mysql> flush privileges;
##查看二进制日志是否打开
mysql> show variables like 'log_%';
+----------------------------------------+--------------------------------+
| Variable_name | Value |
+----------------------------------------+--------------------------------+
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/mysql-bin |
| log_bin_index | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| log_builtin_as_identified_by_password | OFF |
| log_error | /var/log/mysqld.log |
| log_error_verbosity | 3 |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | OFF |
| log_slow_admin_statements | OFF |
| log_slow_slave_statements | OFF |
| log_statements_unsafe_for_binlog | ON |
| log_syslog | OFF |
| log_syslog_facility | daemon |
| log_syslog_include_pid | ON |
| log_syslog_tag | |
| log_throttle_queries_not_using_indexes | 0 |
| log_timestamps | UTC |
| log_warnings | 2 |
+----------------------------------------+--------------------------------+
21 rows in set (0.00 sec)
mysql> show master status;
(1)配置文件(/etc/my.cnf)
[root@server2 ~]# vim /etc/my.cnf
##配置server-id,slave的server-id必须与master不同,即如果开启二进制日志,所有服务器的server-id必须不同
server-id=2
log-bin=mysql-bin
[root@server2 ~]# /etc/init.d/mysqld restart
(2)配置数据库
mysql> change master to master_host='172.25.60.1',master_user='cici',master_password='Westos+110',master_log_file='mysql-bin.000004',master_log_pos=154;
mysql> start slave;
mysql> show slave status\G;
3.防火墙的问题:查看主库防火墙的策略,数据库是否拒绝外来连接,然后做相应的改动
(1)主库:
##创建数据库
mysql> create database user;
mysql> use test;
Database changed
mysql> create table usertable (
-> id varchar(10) not null,
-> username varchar(20) not null);
GTID(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号。它的官方定义如下:
GTID = source_id :transaction_id
每一个 GTID 代表一个数据库事务
在mysql的数据目录(/var/lib/mysql)里查看二进制日志
##mysql-bin.000007为mysql二进制日志,后面为日志码
mysqlbinlog mysql-bin.000007
1、配置配置文件(/etc/my.cnf)并重启mysql(主从都配置一样)
gtid_mode=ON
enforce-gtid-consistency=true
2、配置从数据库
mysql> stop slave;
mysql> change master to master_host='172.25.60.1',master_user='cici',master_password='Westos+110',master_auto_position=1;
mysql> start slave;