1、系統架構html
IP地址 | 安裝服務 | VIP | 主從&hostname |
192.168.16.147 | mysql,keepalived,mha-node | 192.168.16.100 | master |
192.168.16.148 | mysql,keepalived,mha-node | 192.168.16.100 | slave |
192.168.16.149 | mysql,keepalived,mha-node,mha-master,proxysql | 192.168.16.101 | slave |
192.168.16.150 | mysql,keepalived,mha-node,,proxysql | 192.168.16.101 | slave |
2、系統配置node
一、更改主機名(不是必需要改,或者隨意改都行)mysql
hostnamectl set-hostname master hostnamectl set-hostname slave1 hostnamectl set-hostname slave2 hostnamectl set-hostname slave3
二、關閉防火牆和selinuxlinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config systemctl stop firewalld systemctl disable firewalld setenforce 0
3、安裝服務、配置服務git
一、安裝mysqlgithub
二、更改登錄密碼,並部署主從sql
mysql -u root -p
#master alter user 'root'@'localhost' identified by 'ysten123'; create user slave_all@'%' IDENTIFIED by 'Ysten_Slave_01'; grant REPLICATION SLAVE, REPLICATION CLIENT on *.* to slave_all@'%'; show master status; reset master; #slave alter user 'root'@'localhost' identified by 'ysten123'; CHANGE MASTER TO MASTER_HOST='192.168.16.147',MASTER_USER='slave_all',MASTER_PASSWORD='Ysten_Slave_01',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154; start slave; show slave status \G
三、安裝keepalived數據庫
四、在全部機器上安裝mha-node後端
五、在slave2上安裝mha-master服務器
[server default] manager_log=/data/manager/manager.log manager_workdir=/data/manager master_binlog_dir=/data/data/mysql master_ip_failover_script=/usr/local/bin/master_ip_failover password=mha_monitor ping_interval=10 remote_workdir=/data/manager repl_password=Ysten_Slave_01 repl_user=slave_all ssh_port=37322
#ssh_user,若是使用普通用戶,則用建立一個普通用戶再copyid ssh_user=root user=mha_monitor [server1] hostname=192.168.16.147 master_binlog_dir=/data/data/mysql/ port=3306 [server2] candidate_master=1 check_repl_delay=0 hostname=192.168.16.148 master_binlog_dir=/data/data/mysql/ port=3306 [server3] hostname=192.168.16.149 master_binlog_dir=/data/data/mysql/ port=3306 [server4] hostname=192.168.16.150 master_binlog_dir=/data/data/mysql/ port=3306
六、在master數據建立mha數據庫用戶
grant all privileges on *.* to 'mha_monitor'@'192.168.16.149' identified by 'mha_monitor';
七、分別拷貝公鑰至各個服務器,看ssh_user是哪一個用戶,則用哪一個用戶拷貝
八、檢測mha是否正常運行,看日誌,須要檢測經過
masterha_check_ssh --conf=/etc/masterha_default.cnf
masterha_check_repl --conf=/etc/masterha_default.cnf
九、檢測成功後
nohup masterha_manager --conf=/etc/masterha_default.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /data/manager/manager.log 2>&1 &
十、檢測是否啓動成功
masterha_check_status --conf=/etc/masterha_default.cnf
十一、在slave二、slave3安裝proxysql
十二、配置文件,須要更改的proxysql高可用的IP
#file proxysql.cfg ######################################################################################## # This config file is parsed using libconfig , and its grammar is described in: # http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-File-Grammar # Grammar is also copied at the end of this file ######################################################################################## ######################################################################################## # IMPORTANT INFORMATION REGARDING THIS CONFIGURATION FILE: ######################################################################################## # On startup, ProxySQL reads its config file (if present) to determine its datadir. # What happens next depends on if the database file (disk) is present in the defined # datadir (i.e. "/var/lib/proxysql/proxysql.db"). # # If the database file is found, ProxySQL initializes its in-memory configuration from # the persisted on-disk database. So, disk configuration gets loaded into memory and # then propagated towards the runtime configuration. # # If the database file is not found and a config file exists, the config file is parsed # and its content is loaded into the in-memory database, to then be both saved on-disk # database and loaded at runtime. # # IMPORTANT: If a database file is found, the config file is NOT parsed. In this case # ProxySQL initializes its in-memory configuration from the persisted on-disk # database ONLY. In other words, the configuration found in the proxysql.cnf # file is only used to initial the on-disk database read on the first startup. # # In order to FORCE a re-initialise of the on-disk database from the configuration file # the ProxySQL service should be started with "service proxysql initial". # ######################################################################################## datadir="/var/lib/proxysql" #errorlog="/var/lib/proxysql/proxysql.log" admin_variables= { admin_credentials="admin:ysten123;cluster_demo:ysten123" # mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock" mysql_ifaces="0.0.0.0:6032" # refresh_interval=2000 # debug=true cluster_username="cluster_demo" cluster_password="ysten123" cluster_check_interval_ms=200 cluster_check_status_frequency=100 cluster_mysql_query_rules_save_to_disk=true cluster_mysql_servers_save_to_disk=true cluster_mysql_users_save_to_disk=true cluster_proxysql_servers_save_to_disk=true cluster_mysql_query_rules_diffs_before_sync=3 cluster_mysql_servers_diffs_before_sync=3 cluster_mysql_users_diffs_before_sync=3 cluster_proxysql_servers_diffs_before_sync=3 } proxysql_servers= ( { hostname="192.168.16.149" port=6032 weight=0 comment="proxysql-1" }, { hostname="192.168.16.150" port=6032 weight=0 comment="proxysql-2" } ) mysql_variables= { threads=4 max_connections=2048 default_query_delay=0 default_query_timeout=36000000 have_compress=true poll_timeout=2000 # interfaces="0.0.0.0:6033;/tmp/proxysql.sock" interfaces="0.0.0.0:6033" default_schema="information_schema" stacksize=1048576 server_version="5.7.25" connect_timeout_server=3000 # make sure to configure monitor username and password # https://github.com/sysown/proxysql/wiki/Global-variables#mysql-monitor_username-mysql-monitor_password monitor_username="monitor" monitor_password="monitor" monitor_history=600000 monitor_connect_interval=60000 monitor_ping_interval=10000 monitor_read_only_interval=1500 monitor_read_only_timeout=500 ping_interval_server_msec=120000 ping_timeout_server=500 commands_stats=true sessions_sort=true connect_retries_on_failure=10 } # defines all the MySQL servers mysql_servers = ( # { # address = "127.0.0.1" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain # port = 3306 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain # hostgroup = 0 # no default, required # status = "ONLINE" # default: ONLINE # weight = 1 # default: 1 # compression = 0 # default: 0 # max_replication_lag = 10 # default 0 . If greater than 0 and replication lag passes such threshold, the server is shunned # }, # { # address = "/var/lib/mysql/mysql.sock" # port = 0 # hostgroup = 0 # }, # { # address="127.0.0.1" # port=21891 # hostgroup=0 # max_connections=200 # }, # { address="127.0.0.2" , port=3306 , hostgroup=0, max_connections=5 }, # { address="127.0.0.1" , port=21892 , hostgroup=1 }, # { address="127.0.0.1" , port=21893 , hostgroup=1 } # { address="127.0.0.2" , port=3306 , hostgroup=1 }, # { address="127.0.0.3" , port=3306 , hostgroup=1 }, # { address="127.0.0.4" , port=3306 , hostgroup=1 }, # { address="/var/lib/mysql/mysql.sock" , port=0 , hostgroup=1 } ) # defines all the MySQL users mysql_users: ( # { # username = "username" # no default , required # password = "password" # default: '' # default_hostgroup = 0 # default: 0 # active = 1 # default: 1 # }, # { # username = "root" # password = "" # default_hostgroup = 0 # max_connections=1000 # default_schema="test" # active = 1 # }, # { username = "user1" , password = "password" , default_hostgroup = 0 , active = 0 } ) #defines MySQL Query Rules mysql_query_rules: ( # { # rule_id=1 # active=1 # match_pattern="^SELECT .* FOR UPDATE$" # destination_hostgroup=0 # apply=1 # }, # { # rule_id=2 # active=1 # match_pattern="^SELECT" # destination_hostgroup=1 # apply=1 # } ) scheduler= ( # { # id=1 # active=0 # interval_ms=10000 # filename="/var/lib/proxysql/proxysql_galera_checker.sh" # arg1="0" # arg2="0" # arg3="0" # arg4="1" # arg5="/var/lib/proxysql/proxysql_galera_checker.log" # } ) mysql_replication_hostgroups= ( # { # writer_hostgroup=30 # reader_hostgroup=40 # comment="test repl 1" # }, # { # writer_hostgroup=50 # reader_hostgroup=60 # comment="test repl 2" # } ) # http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-File-Grammar # # Below is the BNF grammar for configuration files. Comments and include directives are not part of the grammar, so they are not included here. # # configuration = setting-list | empty # # setting-list = setting | setting-list setting # # setting = name (":" | "=") value (";" | "," | empty) # # value = scalar-value | array | list | group # # value-list = value | value-list "," value # # scalar-value = boolean | integer | integer64 | hex | hex64 | float # | string # # scalar-value-list = scalar-value | scalar-value-list "," scalar-value # # array = "[" (scalar-value-list | empty) "]" # # list = "(" (value-list | empty) ")" # # group = "{" (setting-list | empty) "}" # # empty =
1三、在主庫上建立新用戶,分別對兩臺proxysql受權
GRANT replication client ON *.* TO 'monitor'@'192.168.16.149' IDENTIFIED BY 'monitor';
GRANT replication client ON *.* TO 'monitor'@'192.168.16.150' IDENTIFIED BY 'monitor';
1四、登錄proxysql
#密碼在配置文件中配置 mysql -h127.0.0.1 -P6032 -uadmin -p
1五、操做proxysql
insert into mysql_users(username,password,default_hostgroup,transaction_persistent,default_schema,max_connections)values('yunwei_w','Ysten!3Yw_vas_mas',1,1,'',8096);
#修改參數,這個操做兩臺proxysql都要操做,其餘會同步 update global_variables set variable_value='true' where variable_name='mysql-forward_autocommit'; update global_variables set variable_value='true' where variable_name='mysql-autocommit_false_is_transaction'; update global_variables set variable_value='8096' where variable_name='mysql-max_connections'; load mysql variables to runtime; save mysql variables to disk;
#新增後端數據庫 insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,comment) values(1,'192.168.16.100',3306,100,8096,'Write Group'); insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,comment) values(2,'192.168.16.149',3306,50,8096,'Read Group'); insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,comment) values(2,'192.168。16.150',3306,50,8096,'Read Group'); load mysql servers to runtime; save mysql servers to disk; #添加路由 insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',1,1); insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',2,1); load mysql query rules to runtime; load admin variables to runtime; save mysql query rules to disk; save admin variables to disk;