這篇文章主要講解了“msyql主從備份的過程”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“msyql主從備份的過程”吧!
專業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!創(chuàng)新互聯(lián)公司為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計制作,服務(wù)好的網(wǎng)站設(shè)計公司,網(wǎng)站設(shè)計制作、成都網(wǎng)站制作負責(zé)任的成都網(wǎng)站制作公司!
安裝環(huán)境:
rhel6.5:server1 master
rhel6.5:server2 slave
安裝數(shù)據(jù)庫:
[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.cnf
server-id = n 給服務(wù)器分配一個唯一的ID編號
[root@server1 ~]# /etc/init.d/mysqld start
初始化 MySQL 數(shù)據(jù)庫: [確定]
Installing validate password plugin: [確定]
正在啟動 mysqld: [確定]
[root@server1 ~]# cd /var/lib/mysql
[root@server1 mysql]# cd /var/lib/mysql
[root@server1 mysql]# ls
auto.cnf ibdata1 mysql-bin.000002 performance_schema
ca-key.pem ib_logfile0 mysql-bin.000003 private_key.pem
ca.pem ib_logfile1 mysql-bin.index public_key.pem
client-cert.pem ibtmp1 mysqld_safe.pid server-cert.pem
client-key.pem mysql mysql.sock server-key.pem
ib_bufferpool mysql-bin.000001 mysql.sock.lock sys
[root@server1 mysql]# cat mysql-bin.000001
5.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給服務(wù)器分配一個唯一的ID編號
[root@server1 mysql]# grep password /var/log/mysqld.log
查看密碼
[root@server1 ~]# mysql_secure_installation
初始化
server2同上
server1上授權(quán):
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授權(quán)后,slave可以遠程登錄
[root@server2 ~]# mysql -ucara -p -h 172.25.35.1
mysql> 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.info
25mysql-bin.000004
843
172.25.35.1
cara
Caonimei@478
br/>mysql-bin.000004
843
172.25.35.1
cara
Caonimei@478
60
[root@server2 mysql]# cat server2-relay-bin.index
./server2-relay-bin.000001
master端
查看salve狀態(tài):
mysql> show slave status\G;
1. row
Slave_IO_State:
Master_Host: 172.25.35.1
Master_User: cara
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 843
Relay_Log_File: server1-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: No
Slave_SQL_Running: No
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 master
Master_Host: 172.25.35.1
Master_User: cara
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 843
Relay_Log_File: server1-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
建表
!
插入數(shù)據(jù)
[root@server1 mysql]# mysqlbinlog mysql-bin.000004
查看mster端從的操作
在slave上也可查看master上數(shù)據(jù)
mysql> show tables;
+------------------+
| Tables_in_westos |
+------------------+
| usertb |
+------------------+
mysql> select * from usertb
-> ;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
| user2 | 123 |
+----------+----------+
2 rows in set (0.00 sec)
深入了解復(fù)制-全局事務(wù)標識符(GTID)
GTID的作用
根據(jù)GTID可以知道事務(wù)最初是在哪個實例上提交的
GTID的存在方便了Replication的Failover
GTID比傳統(tǒng)復(fù)制的優(yōu)勢
更簡單的實現(xiàn)failover,不用以前那樣在需要找log_file和log_Pos。
更簡單的搭建主從復(fù)制。
比傳統(tǒng)復(fù)制更加安全。
GTID是連續(xù)沒有空洞的,因此主從庫出現(xiàn)數(shù)據(jù)沖突時,可以用添加空事物的方式進行跳過
[root@server1 mysql]# vim /etc/my.cnf # master和slave都修改
重起兩邊數(shù)據(jù)庫:
[root@server1 mysql]# /etc/init.d/mysqld restart
master端:
slave端:
mysql> stop slave;
mysql> change master to master_host='172.25.35.1',master_user='cara',master_passw
Query 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 event
Master_Host: 172.25.35.1
Master_User: cara
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 154
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> 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端改動些數(shù)據(jù):
slave上查看:
感謝各位的閱讀,以上就是“msyql主從備份的過程”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對msyql主從備份的過程這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!