$ mysql -V #查看mysql版本
mysql Ver 14.14 Distrib 5.5.62, for debian-linux-gnu (x86_64) using readline 6.3***
1.MySQL搭建集群的原因?
a.磁盤瓶頸(數(shù)據(jù)持久化就是將數(shù)據(jù)存入磁盤也稱落盤) 100W/180s = 5500
b.
//將事務(wù)分離 采用集群的方式來做。
//對(duì)比MySQL插入量和MongoDB插入量
600事務(wù) 50000條 100數(shù)量級(jí)別
讀寫分離 讀 --binlog--> 寫
數(shù)據(jù)同步 ---binlog //binlog 是sql語(yǔ)句;這是做集群的基礎(chǔ),錯(cuò)誤語(yǔ)句不會(huì)記錄到binlog
2.分庫(kù)分表
每一個(gè)表里的數(shù)據(jù)是一個(gè)BTree
#創(chuàng)建數(shù)據(jù)庫(kù) create database db_name_0;
#使用庫(kù) use db_name_0;
#創(chuàng)建表 create table tab_name_0;
# 修改配置文件 /etc/mysql/my.cnf
[mysqld]
#127.0.0.1 限制只能本地連接數(shù)據(jù)庫(kù)
#bind-address = 127.0.0.1
bind-address = 0.0.0.0
#啟動(dòng)mysql服務(wù)
sudo /etc/init.d/mysql start
#重啟
sudo /etc/init.d/mysql restart
#登錄
mysql -u root -p
# input password
#1.創(chuàng)建用戶
mysql> create user 'aicken'@'%' identified by '123456';
#2.為用戶授予權(quán)限
mysql> grant all privileges on *.* to 'aicken'@'%' identified by '123456' with grant option;
#刷新數(shù)據(jù)庫(kù) 此步驟不要也行,了解有這個(gè)即可
flush privileges;
#3.重啟mysql
sudo /etc/init.d/mysql restart
# 134 是master 165 是slave
###先配置master
#1.開啟logbin
sudo vim /etc/mysql/my.cnf
[mysqld]
log-bin =mysql-bin
server-id = 101
#2.重啟mysql
sudo /etc/init.d/mysql restart
# aicken -> mysql
#3.創(chuàng)建復(fù)制角色創(chuàng)建復(fù)制用戶
mysql> create user 'replication'@'%' identified by '123456';
mysql> grant all privileges on *.* to 'replication'@'%' identified by '123456' with grant option;
mysql> flush privileges;
#4.查看master狀態(tài)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 457 | | |
+------------------+----------+--------------+------------------+
文件|當(dāng)前的位置
## 配置從新增server-id
[mysqld]
server-id = 102
#2.重啟mysql
sudo /etc/init.d/mysql restart
mysql -u root -p
# 改變數(shù)據(jù)庫(kù)的狀態(tài),不是事務(wù)語(yǔ)句,不會(huì)記錄到binlog文件
mysql> change master to master_host='192.168.215.134',master_port=3306, master_user='replication', master_password='123456',master_log_file='mysql-bin.000001', master_log_pos=457;
# 開始slave
mysql> start slave;
# 查看slave 狀態(tài) \G豎排
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.215.134
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 457
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 457
Relay_Log_Space: 410
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: 101
1 row in set (0.00 sec)
# 現(xiàn)在master和slave可以同步了。