1、環境準備
一、關閉防火牆與selinux
systemctl stop firewalld
setenforce 0
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/confightml
2、mysql5.7.20安裝
#MySQL5.6與MySQL5.7的區別在於初始化的時候,其餘安裝步驟一致
一、建立目錄
mkdir -p /data/mysql/{data,logs,tmp}
touch /data/mysql/{mysql.pid,mysql.sock}
二、建立用戶
useradd mysql
三、安裝依賴包
yum install perl perl-devel perl-Data-Dumper libaio-devel -y
四、下載包
cd /usr/local/src
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
tar zxf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd /usr/local
mv mysql-5.7.20-linux-glibc2.12-x86_64 mysql
chown -R mysql:mysql /usr/local/mysql /data/mysql/mysql
3、配置文件
========================================================linux
[client]
port = 16205
default-character-set=utf8mb4
socket = /data/mysql/mysql.socksql
[mysqld]
user = mysql
port = 16205
server-id = 111
character_set_server=utf8mb4
socket = /data/mysql/mysql.sock
datadir = /data/mysql/data/
pid-file = /data/mysql/mysql.pid數據庫
auto-increment-increment = 2 #主主參數,主從可註釋(必填)
auto-increment-offset = 1 #主主參數,主從可註釋(必填)主:1;從:2vim
#bind-address = 10.26.25.28
#skip-name-resolve=1
#event_scheduler = onbash
federated
skip-ssl
secure-file-priv = /home/mysql
disable-partition-engine-check=1
explicit_defaults_for_timestamp=false服務器
max_allowed_packet = 32M
innodb_file_per_table = 1
back_log = 300
max_connections = 8000
max_connect_errors = 1000
table_open_cache = 4096
open_files_limit = 10240
max_allowed_packet = 512M
wait_timeout = 300
interactive_timeout=300
sort_buffer_size = 16M
join_buffer_size = 16M
query_cache_size = 128M
transaction_isolation = REPEATABLE-READ
thread_stack = 512Ksocket
innodb_buffer_pool_size = 4G
innodb_data_file_path = ibdata1:512M:autoextend
innodb_flush_log_at_trx_commit = 2
innodb_thread_concurrency = 16
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_lock_wait_timeout = 120ide
binlog-ignore-db=information_schema,mysql,performance_schema #主主參數,主從可註釋
#replicate-ignore-db=information_schema,mysql,performance_schema
binlog_format=row
expire_logs_days=10
binlog_cache_size = 7M
log-error = /data/mysql/logs/error.log
log-bin = /data/mysql/logs/mysql_bin.log
log-slave-updates=true #主主參數,主從可註釋
#slow log
slow-query-log = 1
long_query_time = 2
slow-query-log-file = /data/mysql/logs/slowquery.log
sql_mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[mysqld_safe]
pid-file=/data/mysql/mysql.pid
================================================================================
4、初始化
1.MySQL5.7初始化:#注意: 要先初始化,再啓動
初始化: bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql --initialize
查看隨機密碼: cat /data/mysql/logs/error.log | grep password(查看後保存密碼)
2.啓動mysql
bin/mysqld_safe --defaults-file=/etc/my.cnf &
3.修改密碼: /usr/local/mysql/bin/mysqladmin -uroot -p password #輸入隨機密碼,再輸入兩次新密碼便可
# 初始化後數據目錄包含: mysql、information_schema、sys、performache_schema四個數據庫
登陸數據庫:
/usr/local/mysql/bin/mysql -uroot -p
退出數據庫:exit;
5、主從配置
# Master
一、my.cnf
server-id=1111
log-bin=(產生binglog日誌。)
二、建立用於主從同步的帳號
repl(主從同步用戶名)192.168.28.135(從庫所在服務器的IP)
mysql> grant replication slave on *.* to 'repl'@'192.168.28.135' identified by 'a123456';(a123456表示密碼)
Query OK, 0 rows affected, 1 warning (0.06 sec)
mysql> flush privileges;(刷新權限)
Query OK, 0 rows affected (0.02 sec)
三、查看主庫當前bin_log文件和Position
mysql> show master status;
+---------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+--------------------------+-------------------+
| binlog.000008 | 25616 | | information_schema,mysql | |
+---------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
#如下是從庫操做
登陸數據庫
/usr/local/mysql/bin/mysql -uroot -p
四、與主庫創建鏈接
mysql> stop slave;(中止同步)
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.28.136',master_user='repl',master_password='a123456',master_port=16205,master_log_file='mysql_bin.000007',master_log_pos=25616;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
master_host='192.168.28.136’, (主庫所在IP地址)
master_user='repl', (主從同步用戶名)
master_password='a.123456', (主從同步密碼)
master_port=16205, (主庫對外訪問端口號)
master_log_file='mysql_bin.000007',(主庫配置文件)
master_log_pos=25616; (主庫配置文件)
五、start slave;(開啓從庫同步主庫)
六、show slave status;(查看主從同步狀態)show slave status\G
。。。。。。
Slave_IO_Running: Yes (IO線程是否正常)
Slave_SQL_Running: Yes (sql線程是否正常)
#這兩個參數都爲Yes的時候表示主從配置完成
==============================================================================
6、雙主配置(就是把主從反過在配一遍)
登陸數據庫
/usr/local/mysql/bin/mysql -uroot -p
從庫:
二、建立用於主從同步的帳號
repl(主從同步用戶名)192.168.28.135(從庫所在服務器的IP)
mysql> grant replication slave on *.* to 'repl1'@'192.168.28.135' identified by 'a123456';(a123456表示密碼)
Query OK, 0 rows affected, 1 warning (0.06 sec)
mysql> flush privileges;(刷新權限)
Query OK, 0 rows affected (0.02 sec)
三、查看主庫當前bin_log文件和Position
mysql> show master status;
+---------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+--------------------------+-------------------+
| binlog.000008 | 25616 | | information_schema,mysql | |
+---------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
#如下是從庫操做
四、與主庫創建鏈接
mysql> stop slave;(中止同步)
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.28.135',master_user='repl1',master_password='a123456',master_port=16205,master_log_file='mysql_bin.000008',master_log_pos=25616;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
master_host='172.16.1.20’, (主庫所在IP地址)
master_user='repl', (主從同步用戶名)
master_password='a123456', (主從同步密碼)
master_port=16205, (主庫對外訪問端口號)
master_log_file='mysql_bin.000004',(主庫配置文件)
master_log_pos=25618; (主庫配置文件)
五、start slave;(開啓從庫同步主庫)
六、show slave status;(查看主從同步狀態)show slave status\G
。。。。。。
Slave_IO_Running: Yes (IO線程是否正常)
Slave_SQL_Running: Yes (sql線程是否正常)
#這兩個參數都爲Yes的時候表示主從配置完成(兩臺數據庫服務器都爲YES方爲經過,必定要驗證!!!!!!!!!!)
。。。。。。
===========================================================================
如遇訪問權限問題:如下能夠解決
mysql -uroot -p root
mysql->use mysql
mysql->update user set host = '%' where user ='root';
mysql->grant all privileges on *.* to 'root'@'%' with grant option;
mysql->flush privileges;
mysql->exit;
sudo service mysql restart;
**********************************************************************************************************************************
keepalived部署配置(安裝在mysql雙主服務器當中)
1、安裝keepalived
下載keepalived
官網: https://keepalived.org/download.html
上傳並解壓keepalived
/home
cd /home/
tar -zxvf keepalived-2.0.18.tar.gz -C /usr/local/src/
進入目錄/usr/local/src/keepalived-2.0.18
cd /usr/local/src/keepalived-2.0.18/
檢查安裝環境
./configure --prefix=/usr/local/keepalived
第一次檢查:
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
安裝openssl openssl-devel解決問題
yum -y install openssl openssl-devel
第二次檢查
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
安裝libnl libnl-devel解決問題
yum -y install libnl libnl-devel
其餘問題
configure: error: libnfnetlink headers missing
安裝libnfnetlink-devel解決問題
yum -y install libnfnetlink-devel
編譯並安裝
make && make install
將keepalived添加到系統服務中
拷貝執行文件
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
將init.d文件拷貝到etc下,加入開機啓動項
cp /usr/local/src/keepalived-2.0.18/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
將keepalived文件拷貝到etc下
cp /usr/local/src/keepalived-2.0.18/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
建立keepalived文件夾
mkdir -p /etc/keepalived
將keepalived配置文件拷貝到etc下
cp /usr/local/src/keepalived-2.0.18/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
添加keepalived到開機啓動(看我的可隨意)
chkconfig --add keepalived
添加可執行權限
chmod +x /etc/init.d/keepalived
2、部署keepalived雙擊自主切換(一下配置文件和腳本,兩臺機器都要從新布)
備份keepalived配置文件
cp keepalived.conf keepalived.conf.back
從新編輯配置文件
vim keepalived.conf
配置文件
===============================================================
! COnfiguration File for keepalived
global_defs {
router_id MASTER-HA #主機標識
#router_id BACKUP #備機標識
script_user root
enable_script_security
}
vrrp_script chk_mysql_port {
script "/etc/keepalived/chk_mysql.sh" #腳本地址和名字,此處調用改腳本
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER #主機MASTER、備機BACKUP
interface eth0 #本機的網卡
mcast_src_ip 192.168.28.136 #網卡IP
virtual_router_id 51
priority 101 #主機101,備機小於101即可
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.28.188 #新的IP地址,須要在同機網段內
}
track_script {
chk_mysql_port
}
===============================================================================================
編寫腳本
vim chk_mysql.sh
========================================================
#!/bin/bash
#This scripts is check for Mysql Slave status
counter=$(netstat -na|grep "LISTEN"|grep "16205"|wc -l)
if [ "${counter}" -eq 0 ]; then
/etc/init.d/keepalived keepalived stop
killall keepalived
fi
ping 192.168.28.136 -w1 -c1 &>/dev/null
if [ $? -ne 0 ]
then
/etc/init.d/keepalived keepalived stop
killall keepalived
fi
========================================================
添加可執行權限
chmod +x /etc/init.d/keepalived
啓動keepalived
/etc/init.d/keepalived start
查看是否啓動成功
ps -ef | grep keepalived
啓動成功以後會生成一個新的服務器IP地址,可經過新IP直接鏈接數據庫
查看ip地址 ip addr
鏈接數據庫地址
至此,數據庫鏈接成功。
能夠自由關閉其中一臺數據庫服務,keepalived會自動切換到另一臺數據庫服務器當中。