文檔參考: http://blog.csdn.net/u010257584/article/details/51315438mysql
Mysql5.7版本有較大的變化,特別是在mysql主從上面,能夠提供並行複製,因此效率上更高,之後若是有相似需求,須要提供主從複製的話,建議事業部同事選擇5.7的版本。c++
選擇安裝的包都爲源碼包,定義起來較爲方便,因爲所有都爲文字節目操做,因此截圖部分較少。sql
一. mysql 安裝數據庫
1. 基礎包安裝bootstrap
a. 首先清理自帶的mysql相關的包socket
i. yum -y remove mysql-*ide
b. 安裝編譯mysql須要包ui
i. yum install gcc gcc-c++ -y.net
ii. yum install ncurses-devel –y線程
iii. cmake 安裝,版本要求2.8或者3.0以上的
tar xvf cmake*
cd cmake-3.4.3
./configure
make –j 10 ---------------數字爲表cpu核數,並行執行,加快編譯速度
make install
注意:boost也爲mysql編譯必須的包,5.7對其版本也有要求,下載mysql包含boost的就無需手動在安裝了---------下面步驟可略過
tar xvf boost*
cd boost_1_61_0
./bootstrap.sh –prefix=/usr/local/
./b2
./b2 install
2. mysql環境準備
a. mkdir -pv /data/mysql
b. useradd -s /sbin/nologin mysql
c. chown -R mysql.mysql /data/mysql
3. 編譯安裝mysql
a. tar xvf mysql-boost-5.7.12.tar.gz
b. cd mysql-5.7.12
c. cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=/opt/package/boost_1_59_0/ ----------制定mysql安裝目錄、默認字符集、boost路徑
d. make -j 40 && make install
4. mysql初始化
a. 創建mysql配置文件 /etc/my.cnf -----不管需不須要配置主從,都建議先把server-id以及log-bin配置了,避免之後有添加配置主從的要求
拷貝配置文件模板爲新的mysql配置文件
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
# cat /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
pid-file = /data/mysql/mysql.pid
user = mysql
server-id = 29
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
#############for mysql replication##########
log-bin = mysql-bin
log-bin-index = mysql-bin
relay-log = relay-log
relay-log-index = relay-log
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
###############################################33
expire_logs_days = 30
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
b. /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql -------注意5.7以前的版本用mysql_install_db初始化
例如:./scripts/mysql_install_db --datadir=/data/mysql5.6/ --user=mysql --basedir=/usr/local/mysql5.6/ --defaults-file=/etc/my.cnf.5.6
啓動: /usr/local/mysql5.6/bin/mysqld_safe --defaults-file=/etc/my56.cnf &
關閉:mysqladmin –u root -h 127.0.0.1 –port 3307 shutdown
mysqladmin -uroot -p1234#Byd -S /tmp/mysql5.6.sock shutdown
安裝多個實例,舊版本初始化時候要指定—defaults-file
而後初始化密碼時候也要指定對應的端口或者socket,例如:
/usr/local/mysql5.6/bin/mysqladmin -u root -h 127.0.0.1 --port 3307 password 'password'
/usr/local/mysql5.6/bin/mysqladmin -u root --socket --port 3307 password 'password'
c. 啓動mysql, /usr/local/mysql/bin/mysqld_safe &
d. 設置mysql root登錄密碼:/usr/local/mysql/bin/mysql -e "grant all privileges on *.* to root@'127.0.0.1' identified by \"1234#Byd\" with grant option;"
/usr/local/mysql/bin/mysql -e "grant all privileges on *.* to root@'localhost' identified by \"1234#Byd\" with grant option;"
e. 其餘,配置mysql服務以及mysql bin環境變量
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod 755 !$
能夠經過 service mysql start/status/stop 管理
ln -s /usr/local/mysql/bin/* /usr/local/bin/
登錄Mysql:mysql -u root -p1234#Byd
二. 主從配置
1. 安裝上面的操做配置主從,先把mysql先配置了(下面主從僅適用於新安裝的,已有的數據庫,要求有配置server-id,以及開啓二進制日誌的前提下才能夠配置主從,須要先把主庫導出,而後配置同步)
2. 主從差別,配置文件server-id不一樣便可
3. 在主庫上建立用於複製的用戶
grant replication slave on *.* to slave@'10.9.31.%' identified by 'slave#2016?';
4. 主庫查詢狀態:查詢當前日誌文件及position,用於日誌恢復
五、從庫配置到主庫的複製
change master to master_host='10.9.31.27',master_user='slave',master_password='slave#2016?',master_log_file='mysql-bin.000004',master_log_pos=1225;
開啓複製:start slave;
查看複製狀態:show slave status\G; 查詢到複製的IO和SQL線程都爲YES表示配置成功。
6. 驗證
在主庫建立庫或者表,在從庫查詢。
注意:5.6以上版本開啓了GTID特性,mysql本身識別複製log及pos,方便管理;因爲上面my.cnf已經開啓了gtid,可使用該特性完成複製
1:配置帳號
grant replication slave,replication client on *.* to repl@'10.9.31.29' identified by 'Repl#2016';
change master to master_host='10.9.31.26',master_port=3306,master_user='repl',master_password='Repl#2016',master_auto_position=1;
啓動複製
start slave;
show slave status\G;
show processeslist;