非常感謝大家昨晚來到我們的公開課課堂,一起探討了關於數據庫的架構,最後詳細探討學習了mysql主從同步的搭建過程。
今天分享一下昨晚的mysql數據庫主從搭建的課堂筆記,讓更多的人因爲這次課程受益,另外我們每隔一段時間都會有相應的互動課程,大家可以前往https://ke.qq.com/course/254420#tuin=b66303d9 騰訊課堂鏈接報名參加。
以下是本次課程的課堂筆記,主題聊聊數據庫架構那些事
首先分享了同方,從14年到17年的數據庫架構,接下來是關於數據庫主從的搭建
原理圖如下:
mysql主從複製中:
第一步:master記錄二進制日誌。在每個事務更新數據完成之前,master在二日誌記錄這些改變。MySQL將事務寫入二進制日誌,即使事務中的語句都是交叉執行的。在事件寫入二進制日誌完成後,master通知存儲引擎提交事務。 第二步:slave將master的binary log拷貝到它自己的中繼日誌。首先,slave開始一個工作線程——I/O線程。I/O線程在master上打開一個普通的連接,然後開始binlog dump process。Binlog dump process從master的二進制日誌中讀取事件,如果已經執行完master產生的所有文件,它會睡眠並等待master產生新的事件。I/O線程將這些事件寫入中繼日誌。 第三步:SQL slave thread(SQL從線程)處理該過程的最後一步。SQL線程從中繼日誌讀取事件,並重新執行其中的事件而更新slave的數據,使其與master中的數據一致。只要該線程與I/O線程保持一致,中繼日誌通常會位於OS的緩存中,所以中繼日誌的開銷很小。 此外,在master中也有一個工作線程和其它MySQL的連接一樣,slave在master中打開一個連接也會使得master開始一個線程。複製過程有一個很重要的限制——複製在slave上是串行化的,也就是說master上的並行更新操作不能在slave上並行操作。
詳細過程:
模式: C/S 模式
端口:
3306
x-63 主mysql服務器配置
創建要同步的數據庫:
mysql> create database bawei;
mysql> use bawei;
mysql> create table test1 (id int);
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bawei |
| mysql |
| test |
停止
#service mysqld stop
編輯
#vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0 #在原配置文件中,添加以下內容:
log-bin=mysqllog #啓用二進制日誌,默認存在/var/lib/mysql 下面
server-id=1 #本機數據庫ID 標示。其中master_id必須爲1到232之間的一個正整數值
binlog-do-db=bawei #可以被從服務器複製的庫。二進制需要同步的數據庫名
# binlog-ignore-db=bawei2 不可以被從服務器複製的庫
重新啓動
#service mysqld restart
授權
mysql> grant replication slave on *.* to [email protected] identified by "123456";
查看狀態,信息
mysql> show master status;
+-----------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------+----------+--------------+------------------+
| mysqllog.000001 | 242 | bawei | |
+-----------------+----------+--------------+------------------+
1 row in set (0.00 sec)
查看二進制日誌位置:
[root@x-63 ~]# ls /var/lib/mysql/
ibdata1 ib_logfile1 mysql mysqllog.index passwd
ib_logfile0 bawei mysqllog.000001 mysql.sock test
mysql>show binlog events \G
複製前保證兩個數據庫數據一致:
mysqldump -u root -p123456 -A >all.sql
Enter password:
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
把數據傳給從:
scp all.sql 192.168.1.64:/root
配置從服務器:
mysql從服務器: x-64
[root@x-64 opt]# yum install mysql-server -y
[root@x-64 opt]# service mysqld start
mysql>show variables like '%version%';查看版本
+-------------------------+---------------------+
| Variable_name | Value |
+-------------------------+---------------------+
| protocol_version | 10 |
| version | 5.1.52 |
| version_comment | Source distribution |
| version_compile_machine | x86_64 |
| version_compile_os | redhat-linux-gnu |
+-------------------------+---------------------+
5 rows in set (0.03 sec)
測試連接到主服務器是否成功
[root@x-64 opt]# mysql -u slave -h 192.168.1.63 -p123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
看不到bawei數據庫
導入數據庫,和主服務器保持一致
[root@x-64 ~]# mysql -u root -p < all.sql
從服務器沒必要開bin-log日誌注。
修改從服務器配置文件:
[root@x-64 ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#在配置文件中寫入以下內容
server-id=2 #從服務器ID號,不要和主ID相同 ,如果設置多個從服務器,每個從服務器必須有一個唯一的server-id值,必須與主服務器的以及其它從服務器的不相同。可以認爲server-id值類似於IP地址:這些ID值能唯一識別複製服務器羣集中的每個服務器實例。
master-host=192.168.1.63 #指定主服務器IP地址
master-user=slave #制定在主服務器上可以進行同步的用戶名
master-password=123456 #密碼
####以下可以不寫
master-port = 3306 #同步所用的端口
master-connect-retry=60 #斷點重新連接時間
保存,重啓
#service mysqld restart
如果只做爲備庫,就只設置server-ID, 如果他也作爲一個分發主庫,開啓bin-log和中繼日誌
測試:
主服務器上查看:
mysql> show master status;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 2
Current database: bawei1
+-----------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------+----------+--------------+------------------+
| mysqllog.000004 | 106 | bawei1 | |
+-----------------+----------+--------------+------------------+
從服務器上查看:
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.63
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqllog.000001
Read_Master_Log_Pos: 315
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 459
Relay_Master_Log_File: mysqllog.000001
Slave_IO_Running: Yes #可以看到這兩個Yes,說明從服務器安裝成功。
Slave_SQL_Running: Yes
Slave_IO_Running :一個負責與主機的io通信
Slave_SQL_Running:負責自己的slave mysql進程
mysql> START SLAVE
測試:數據同步
x-63寫數據:
mysql> use bawei;
Database changed
mysql> show tables;
+--------------+
| Tables_in_bawei |
+--------------+
| test1 |
+--------------+
1 row in set (0.00 sec)
mysql> insert into test1 values(1);
x-64讀數據:
mysql> use bawei;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from test1;
+------+
| id |
+------+
| 1 |
+------+
到現在爲止mysql主從同步就搭建完成了,因爲篇幅限制,後面會出現的問題解決方案暫時就不在這裏闡述了,如果有興趣的可以到公開課上一起探討。