1、安裝mysqlmysql
yum list installed | grep mariadb yum remove mariadb-libs.x86_64 而且移除/etc/my.cnf文件 rm -r /etc/my.cnfgit
因爲mariadb是mysql的一個分支在centos7中會默認安裝該數據庫,須要將其寫卸載spring
2.mysql5.7.19版本須要安裝libnuma相關的依賴包sql
yum list installed |grep numactl 查詢是否安裝數據庫
yum install numactl 安裝vim
3.解壓mysql5.7.17到/usr/local/mysql下(路徑可自行指定)centos
groupadd mysql useradd -g mysql mysql passwd mysqlspringboot
檢查指定mysql組是否存在:more /etc/group | grep mysql服務器
檢查指定用戶所屬組:groups mysqlapp
檢查當前活躍(在線)的用戶列表:w
4.檢查是否安裝了 libaio
rpm -qa | grep libaio yum -y install libaio
5.配置my.cnf文件
cp my-default.cnf /etc/my.cnf
安裝:./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
注意須要記住:A temporary password is generated for root@localhost: =:,tUa2Yvr(7
啓動腳本添加到資源目錄中:cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
受權 chmod +x /etc/rc.d/init.d/mysqld
添加到系統服務 chkconfig --add mysqld
校驗服務是否生效 chkconfig --list mysqld 其中2 3 4 5代表開機啓動
啓動命令: service mysqld start
添加環境變量:vim /etc/profile 注意添加完後須要刷新配置信息 source /etc/profile
登陸 mysql -uroot -p
登陸成功後設置密碼 SET PASSWORD = PASSWORD('123456');(初次登陸)
受權遠程訪問 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
建立只讀帳號: GRANT Select ON *.* TO 'readonly'@'%' IDENTIFIED BY "123456";
建立可寫帳號: GRANT ALL PRIVILEGES ON *.* TO 'write'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
關閉臨時防火牆:systemctl stop firewalld
在搭建一臺備份mysql服務器(參照上面便可)
2、經過 wget安裝
yum list installed | grep mariadb yum remove mariadb-libs.x86_64 而且移除/etc/my.cnf文件 rm -r /etc/my.cnf (與上面第一點相同)
2. 下載mysql的repo
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
安裝插件 yum -y install wget(沒有wget命令時安裝)
3.安裝 sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
4.安裝mysql sudo yum install mysql-server
注意:若報錯:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' 表示沒有權限:chown root /var/lib/mysql/ 而後重啓mysql:service mysqld restart
5.登陸:mysql -u root -p 修改密碼 update user set password=password('123456') where user='root';
6.重啓 service mysqld restart
7.設置遠程登陸:GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";
8.重啓並關閉防火牆 service mysqld restart systemctl stop firewalld.service
3、主節點配置
2.重啓mysql:service mysqld restart
3.驗證配置信息是否成功 show variables like '%service_id%' ; show master status;
4、從節點配置
1.克隆主節點
2.修改配置文件:vim /etc/my.cnf
server_id=122 log-bin=mysql-bin binlog_do_db=test #須要同步的數據庫,多個使用逗號分隔
3.重啓
4.從節點中配置同步信息
change master to master_host='主mysql_ip',master_user='root',master_password='123456',
master_log_file='mysql-bin.000002',master_log_pos=216;
其中master_log_file 和master_log_pos 表示主mysql文件名,和同步位置,使用show master status;在主節點出現
5.開始同步 : start slave
6.檢查同步狀態:show slave status;
注意若是是使用虛擬機克隆功能在複製從節點,同步時下面多是no
是因爲克隆時/etc/my.cnf中UUID重複了。rm -rf /var/lib/mysql/auto.cnf 而後重啓便可。
5、主從同步原理
藉助binlog日誌文件中的SQL執行命令實現主從複製,即master節點執行完一條SQL命令經過日誌形式在salve節點在執行一次,在從庫中有兩個線程一個I/O和一個SQL線程,I/O線程主要去請求主庫的binlog日誌並將日誌寫到relay log中。主庫會生成一個log dump線程,用來給I/O線程傳遞binlog,SQL線程讀取relay log文件中的日誌,並解析成相應的操做。
6、springboot讀寫分離
gitlab源碼:http://47.93.254.162:8090/open/dynamicDataSource
1.原理:配置讀寫兩個動態數據源,經過AOP攔截service方法,判斷方法名若是是查詢開頭(get、select、find等)注入只讀數據源,不然注入可寫數據源。數據源裝配類須要繼承AbstractRoutingDataSource,講當前數據源交給spring管理。
2.核心實現方法
application.xml
1 spring: 2 datasource: 3 #可寫數據源 4 readpnly: 5 jdbc-url: jdbc:mysql://192.168.0.112:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false 6 driver-class-name: com.mysql.jdbc.Driver 7 username: root 8 password: 123456 9 #可讀數據源 10 write: 11 jdbc-url: jdbc:mysql://192.168.0.113:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false 12 driver-class-name: com.mysql.jdbc.Driver 13 username: readonly 14 password: 123456 15 type: com.alibaba.druid.pool.DruidDataSource 16 server: 17 port: 8100
數據源配置類
1 @Configuration 2 public class DatasourceConfig { 3 4 @Bean(name = "readDataSource") 5 @ConfigurationProperties(prefix = "spring.datasource.readpnly") 6 public DataSource readDataSource() { 7 return DataSourceBuilder.create().build(); 8 } 9 10 @Bean(name = "writeDataSource") 11 @ConfigurationProperties(prefix = "spring.datasource.write") 12 public DataSource writeDataSource() { 13 return DataSourceBuilder.create().build(); 14 } 15 }
數據源注入類
1 @Component 2 @Lazy(false) 3 public class DatasourceContextConfig { 4 5 private static final ThreadLocal<String> datasouceContext = new ThreadLocal<>(); 6 7 // 設置數據源 8 public static void setDatasource(String datasourceName) { 9 datasouceContext.set(datasourceName); 10 } 11 12 // 獲取數據源 13 public static String getDatasource() { 14 return datasouceContext.get(); 15 } 16 17 // 清除數據源 18 public static void clearDatasource() { 19 datasouceContext.remove(); 20 } 21 }
數據源與spring交互類
1 @Component 2 @Primary 3 public class DynamicDatasourceConfig extends AbstractRoutingDataSource { 4 5 @Autowired 6 @Qualifier("readDataSource") 7 private DataSource readDataSource; 8 9 @Autowired 10 @Qualifier("writeDataSource") 11 private DataSource writeDataSource; 12 13 @Override 14 protected Object determineCurrentLookupKey() { 15 16 System.out.println("當前開始注入數據源 :" + DatasourceContextConfig.getDatasource()); 17 18 return DatasourceContextConfig.getDatasource(); 19 } 20 21 @Override 22 public void afterPropertiesSet() { 23 Map<Object, Object> map = new HashMap<>(); 24 map.put("readDataSource", readDataSource); 25 map.put("writeDataSource", writeDataSource); 26 setTargetDataSources(map); 27 setDefaultTargetDataSource(writeDataSource); 28 super.afterPropertiesSet(); 29 } 30 }
AOP攔截類
1 @Aspect 2 @Component 3 public class DynamicDatasourceAopConfig { 4 5 // 攔截service方法,根據方法名判斷注入讀寫數據源 6 @Before("execution(* com.dynamic.service.*.*(..))") 7 public void process(JoinPoint joinPoint) { 8 String methodName = joinPoint.getSignature().getName(); 9 if(methodName.startsWith("git") || methodName.startsWith("count") || 10 methodName.startsWith("select") || methodName.startsWith("find")) { 11 DatasourceContextConfig.setDatasource("readDatasource"); 12 } else { 13 DatasourceContextConfig.setDatasource("writeDatasource"); 14 } 15 } 16 17 }
測試
7、MyCat搭建
gitlab地址:http://47.93.254.162:8090/open/mycat.git
1.下載mycat:http://dl.mycat.io/1.6.5/
2.在cnf文件下有:server.xml :Mycat配置文件,包含帳號、參數等信息
schema.xml: Mycat對應實際數據庫和表的配置
rule.xml : Mycat水平分庫分表規則
3../mycat start 啓動 ./mycat stop 中止 ./mycat restart 重啓
注意:當/mycat/logs wrapper.log中 爲successfully 則啓動成功
4.其餘代碼和 springboot讀寫分離 保持一致,只需換數據庫鏈接地址便可
8、一次線上 從服務器停電後的同步