本文共 5242 字,大约阅读时间需要 17 分钟。
安装环境:
rhel6.5:server1 masterrhel6.5:server2 slave安装数据库:[root@server1 mysql]# yum install -y 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 [root@server1 ~]# vim /etc/my.cnfserver-id = n 给服务器分配一个唯一的ID编号[root@server1 ~]# /etc/init.d/mysqld start初始化 MySQL 数据库: [确定]Installing validate password plugin: [确定]正在启动 mysqld: [确定][root@server1 ~]# cd /var/lib/mysql[root@server1 mysql]# cd /var/lib/mysql[root@server1 mysql]# lsauto.cnf ibdata1 mysql-bin.000002 performance_schemaca-key.pem ib_logfile0 mysql-bin.000003 private_key.pemca.pem ib_logfile1 mysql-bin.index public_key.pemclient-cert.pem ibtmp1 mysqld_safe.pid server-cert.pemclient-key.pem mysql mysql.sock server-key.pemib_bufferpool mysql-bin.000001 mysql.sock.lock sys[root@server1 mysql]# cat mysql-bin.0000015.7.17-log[8**4���[#���e�[�)�[root@server1 mysql]# ll mysql-bin.000001
-rw-r----- 1 mysql mysql 177 7月 28 14:40 mysql-bin.000001修改slave配置文件
server-id = n给服务器分配一个唯一的ID编号[root@server1 mysql]# grep password /var/log/mysqld.log 查看密码[root@server1 ~]# mysql_secure_installation 初始化server2同上server1上授权:mysql> grant replication slave on . to cara@'172.25.35.2' identified by 'Caonimei@478';Query OK, 0 rows affected, 1 warning (0.02 sec)mysql> flush privileges; 刷新
Query OK, 0 rows affected (0.02 sec)master授权后,slave可以远程登录[root@server2 ~]# mysql -ucara -p -h 172.25.35.1mysql> show databases;+--------------------+| Database |+--------------------+| information_schema |+--------------------+1 row in set (0.00 sec)master上查看:mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000004 | 843 | | | |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)使 slave 与 master 建立连接,从而同步:mysql> change master to master_host='172.25.35.1',master_user='cara',master_password='Caonimei@478',master_log_file='mysql-bin.000004',master_log_pos=843;slave上查看:mysql> show databases-> ;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+mysql> change master to master_host='172.25.35.1',master_user='cara',master_password='Caonimei@478',master_log_file='mysql-bin.000004',master_log_pos=843;[root@server2 mysql]# cd /var/lib/mysql[root@server2 mysql]# cat master.info25
mysql> start slave;(mster和slave都需开启)
Query OK, 0 rows affected (0.01 sec)mysql> show slave status\G;
1. row Slave_IO_State: Connecting to masterMaster_Host: 172.25.35.1Master_User: caraMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000004Read_Master_Log_Pos: 843Relay_Log_File: server1-relay-bin.000001Relay_Log_Pos: 4Relay_Master_Log_File: mysql-bin.000004Slave_IO_Running: ConnectingSlave_SQL_Running: Yes建表!插入数据[root@server1 mysql]# mysqlbinlog mysql-bin.000004查看mster端从的操作在slave上也可查看master上数据mysql> show tables;+------------------+| Tables_in_westos |+------------------+| usertb |+------------------+mysql> select * from usertb-> ;+----------+----------+| username | password |+----------+----------+| user1 | 123 || user2 | 123 |+----------+----------+2 rows in set (0.00 sec)深入了解复制-全局事务标识符(GTID)GTID的作用根据GTID可以知道事务最初是在哪个实例上提交的
GTID的存在方便了Replication的Failover
GTID比传统复制的优势更简单的实现failover,不用以前那样在需要找log_file和log_Pos。
更简单的搭建主从复制。
比传统复制更加安全。
GTID是连续没有空洞的,因此主从库出现数据冲突时,可以用添加空事物的方式进行跳过
[root@server1 mysql]# vim /etc/my.cnf # master和slave都修改重起两边数据库:[root@server1 mysql]# /etc/init.d/mysqld restartmaster端:slave端:mysql> stop slave;mysql> change master to master_host='172.25.35.1',master_user='cara',master_passwQuery OK, 0 rows affected, 2 warnings (0.08 sec)mysql> start slave; Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
1. row Slave_IO_State: Waiting for master to send eventMaster_Host: 172.25.35.1Master_User: caraMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000005Read_Master_Log_Pos: 154Relay_Log_File: server2-relay-bin.000002Relay_Log_Pos: 367Relay_Master_Log_File: mysql-bin.000005Slave_IO_Running: YesSlave_SQL_Running: Yesmysql> use mysql;mysql> show tables;+---------------------------+| Tables_in_mysql |+---------------------------+| columns_priv || db || engine_cost || event || func || general_log || gtid_executed || help_category || help_keyword || help_relation || help_topic || innodb_index_stats || innodb_table_stats || ndb_binlog_index || plugin || proc || procs_priv || proxies_priv || server_cost || servers || slave_master_info || slave_relay_log_info || slave_worker_info || slow_log || tables_priv || time_zone || time_zone_leap_second || time_zone_name || time_zone_transition || time_zone_transition_type || user |+---------------------------+31 rows in set (0.00 sec)master端改动些数据: slave上查看:转载于:https://blog.51cto.com/13810716/2151506