狂創客圈 經典圖書 : 《Netty Zookeeper Redis 高併發實戰》 面試必備 + 面試必備 + 面試必備 【博客園總入口 】html
瘋狂創客圈 經典圖書 : 《SpringCloud、Nginx高併發核心編程》 大廠必備 + 大廠必備 + 大廠必備 【博客園總入口 】java
入大廠+漲工資必備: 高併發【 億級流量IM實戰】 實戰系列 【 SpringCloud Nginx秒殺】 實戰系列 【博客園總入口 】mysql
組件 | 連接地址 |
---|---|
【必須】 虛擬機Linux 開發環境準備 | https://www.cnblogs.com/crazymakercircle/p/14194688.html |
Linux openresty 安裝 | Linux openresty 安裝 |
【必須】Linux Redis 安裝(帶視頻) | Linux Redis 安裝(帶視頻) |
【必須】Linux Zookeeper 安裝(帶視頻) | Linux Zookeeper 安裝, 帶視頻 |
Windows Redis 安裝(帶視頻) | Windows Redis 安裝(帶視頻) |
RabbitMQ 離線安裝(帶視頻) | RabbitMQ 離線安裝(帶視頻) |
ElasticSearch 安裝, 帶視頻 | ElasticSearch 安裝, 帶視頻 |
Nacos 安裝(帶視頻) | Nacos 安裝(帶視頻) |
【必須】Eureka | Eureka 入門,帶視頻 |
【必須】springcloud Config 入門,帶視頻 | springcloud Config 入門,帶視頻 |
【必須】SpringCloud 腳手架打包與啓動 | SpringCloud腳手架打包與啓動 |
Linux 自啓動 假死自啓動 定時自啓 | Linux 自啓動 假死啓動 |
組件 | 連接地址 |
---|---|
準備一: 在window安裝虛擬機集羣 | vagrant+java+springcloud+redis+zookeeper鏡像下載(&製做詳解)) |
並且:在虛擬機上須要安裝 mysql | centos mysql 筆記(內含vagrant mysql 鏡像) |
分庫分表 Sharding-JDBC 從入門到精通之一 | Sharding-JDBC 入門實戰 |
分庫分表 Sharding-JDBC 從入門到精通之二 | Sharding-JDBC 基本原理 |
分庫分表 Sharding-JDBC 從入門到精通之三 | MYSQL主從複製 |
分庫分表 Sharding-JDBC 從入門到精通之四 | 讀寫分離 |
分庫分表 Sharding-JDBC 從入門到精通之源碼 | git |
主從實戰的數據庫場景:git
建議有三虛擬機:cdh一、 cdh二、 cdh3(cdh3能夠不開啓)。面試
cdh1 上的 user_db爲主,cdh1 上的 user_db爲從,僅僅開啓這兩個庫的複製,其餘的庫如store,不開啓主從複製。
有關學習環境的快速搭建,請參考本系列博客的準備一: 在window安裝虛擬機集羣 。redis
登陸主庫spring
mysql ‐uroot ‐p123456
建立用於主從複製的帳號sql
set global validate_password_policy=0; set global validate_password_length=1; create user db_sync identified by '123456';
建立庫和表數據庫
mysql> CREATE DATABASE user_db DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; Query OK, 1 row affected (0.00 sec) mysql> use user_db; Database changed mysql> create table t_user_0( id bigint , name varchar(40) ); Query OK, 0 rows affected (0.02 sec)
受權主備複製專用帳號編程
GRANT REPLICATION SLAVE ON *.* TO 'db_sync'@'%' IDENTIFIED BY 'db_sync';
刷新權限
FLUSH PRIVILEGES;
修改 MySQL 配置文檔 /etc/my.cnf,在 [mysqld] 段添加如下配置:
[mysqld] #開啓日誌,啓用二進制日誌 log‐bin = mysql‐bin #設置服務器標識ID,主從不能一致 ,每臺服務器惟一 server‐id = 1 #設置須要同步的數據庫 binlog‐do‐db=user_db #屏蔽不須要同步的數據庫 binlog‐ignore‐db=mysql binlog‐ignore‐db=information_schema binlog‐ignore‐db=performance_schema binlog‐ignore‐db=dolphinscheduler binlog‐ignore‐db=sys binlog‐ignore‐db=store
參數說明:
log-bin
該參數只要配置就表示開啓了MySQL的bin log日誌功能,注意改參數的值是咱們自定義的,咱們自定義的值將做爲bin log的名稱的前綴信息喲,咱們可使用MySQL命令"show variables like '%log_bin%';"查看我們的配置。server-id
該參數能夠指定數據庫服務器的惟一標識。在同一個複製組下的全部實例的server_id都必須是惟一的,並且取值必須是正整數,取值範圍是1~(232)−1
能夠經過show databases,查看全部的庫,而後找出不須要同步的數據庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | dolphinscheduler | | mysql | | performance_schema | | store | | sys | +--------------------+ 6 rows in set (0.01 sec)
完整配置以下:
[root@cdh1 ~]# cat /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] log-bin=user-mysql-bin server-id=1 binlog-do-db=user_db binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-ignore-db=performance_schema binlog-ignore-db=dolphinscheduler binlog-ignore-db=sys binlog-ignore-db=store skip-name-resolve character_set_server=utf8 init_connect='SET NAMES utf8' lower_case_table_names=1 # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
登陸從庫
mysql ‐uroot ‐p123456
建立用於主從複製的帳號
set global validate_password_policy=0; set global validate_password_length=1; create user db_sync identified by '123456';
建立庫和表
mysql> CREATE DATABASE user_db DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; Query OK, 1 row affected (0.00 sec) mysql> use user_db; Database changed mysql> create table t_user_0( id bigint , name varchar(40) ); Query OK, 0 rows affected (0.02 sec)
受權主備複製專用帳號
GRANT REPLICATION SLAVE ON *.* TO 'db_sync'@'%' IDENTIFIED BY '123456';
刷新權限
FLUSH PRIVILEGES;
修改 MySQL 配置文檔 /etc/my.cnf,在 [mysqld] 段添加如下配置:
[mysqld] log‐bin=mysql‐bin #開啓日誌,啓用二進制日誌 server‐id=2 #設置服務器標識ID,主從不能一致 ,每臺服務器惟一 #設置須要同步的數據庫 replicate_wild_do_table=user_db.% #屏蔽系統庫同步 replicate_wild_ignore_table=mysql.% replicate_wild_ignore_table=information_schema.% replicate_wild_ignore_table=performance_schema.% replicate_wild_ignore_table=dolphinscheduler.% replicate_wild_ignore_table=sys.% replicate_wild_ignore_table=store.%
重啓數據庫
systemctl restart mysqld
完整的配置文件以下:
[root@cdh2 ~]# vim /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] log-bin=user-mysql-bin server-id=101 replicate_wild_do_table=user_db.% replicate_wild_ignore_table=mysql.% replicate_wild_ignore_table=information_schema.% replicate_wild_ignore_table=performance_schema.% replicate_wild_ignore_table=dolphinscheduler.% replicate_wild_ignore_table=sys.% replicate_wild_ignore_table=store.% skip-name-resolve character_set_server=utf8 init_connect='SET NAMES utf8' lower_case_table_names=1 # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0
首先鏈接 master db,爲從庫的帳號授予複製的權利
GRANT REPLICATION SLAVE ON *.* TO 'db_sync'@'%' IDENTIFIED BY '123456';
若是權限不夠,就執行一下下面的兩句:
set global validate_password_policy=0; set global validate_password_length=1;
刷新權限
FLUSH PRIVILEGES;
在主庫查看同步的起點, 記錄下文件名以及起點 位置
show master status;
查看數據庫狀態:
記錄 File 的值( user-mysql-bin.000001 )和 Position 的值(154),等會配置 slave 服務器的時候要用。
接下來在 slave db上,配置主從複製:
先中止同步
STOP SLAVE;
修改從庫的master配置,指向到主庫,配置過程當中,須要使用上一步記錄的文件名以及位點
mysql>change master to master_host='cdh1', master_user='db_sync', master_log_file='user-mysql-bin.000001', master_log_pos=154, master_port=3306, master_password='123456'; Query OK, 0 rows affected, 2 warnings (0.03 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec)
解釋下配置參數
master_host='x.x.x.x' // 這裏填 master 主機 ip master_log_file='user-mysql-bin.000001', // 這裏填寫 File 的值 master_log_pos=154,// 這裏填寫 Position 的值。
啓動同步
START SLAVE;
查看同步
show slave status\G;
結果以下:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: cdh1 Master_User: db_sync Master_Port: 3306 Connect_Retry: 60 Master_Log_File: user-mysql-bin.000002 Read_Master_Log_Pos: 154 Relay_Log_File: cdh2-relay-bin.000004 Relay_Log_Pos: 377 Relay_Master_Log_File: user-mysql-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: user_db.% Replicate_Wild_Ignore_Table: mysql.%,information_schema.%,performance_schema.%,dolphinscheduler.%,sys.%,store.% Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 154 Relay_Log_Space: 2531 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: fb8d483c-47eb-11eb-ba17-0800276c3e95 Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
查看Slave_IO_Runing和Slave_SQL_Runing字段值都爲Yes,表示同步配置成功。若是不爲Yes,請排
查相關異常。
若是不當心配置錯, 輸入 mysql> stop slave; 而後從新錄入一遍就能夠了。
問題1: 因爲是克隆的虛擬機,因此mysql的uuid相同,報錯以下:
The slave I/O thread stops because master and slave have equal MySQL server UUID
緣由:主從複製的mysql實例的uuid不能相同。
修改方法:在從庫上,從my.cnf找到data目錄,而後定位到auto.cnf目錄,找到 auto.cnf文件,進行uuid的調整。
具體以下:
$ vim /var/lib/mysql/auto.cnf [auto] server-uuid=fb8d483c-47eb-11eb-ba17-0800276c3e96
修改以後:
[root@cdh2 ~]# cat /var/lib/mysql/auto.cnf [auto] server-uuid=fb8d483c-47eb-11eb-ba17-0800276c3e96
最終,確保兩個mysql 實例的uuid不一樣。
而後,重啓改動過的實例。
1 將主庫的t_user_0表複製一份t_user_0_copy1,稍等一會,刷新從庫,發現從庫也有了一個t_user_0_copy1表,代表從庫複製了主庫的t_user_0_copy1表。
2 修改主庫的t_user_0_copy1,名稱爲t_user_1,大約過去10s,刷新從庫,從庫的t_user_0_copy1名稱改成t_user_1
瘋狂創客圈 - Java高併發研習社羣,爲你們開啓大廠之門