一级及、環(huán)境:兩臺LINUX7 64位服務(wù)器己儒;
IP:192.168.200.199,192.168.200.200记焊,199為主誉帅,200為從。
二氧卧、原理:
主服務(wù)器(Master)負責網(wǎng)站寫操作桃笙,從服務(wù)器負責讀操作氏堤。主從服務(wù)器利用MySQL的二進制日志文件沙绝,實現(xiàn)數(shù)據(jù)同步。二進制日志由主服務(wù)器產(chǎn)生鼠锈,從服務(wù)器響應(yīng)獲取同步數(shù)據(jù)庫闪檬。
三、安裝mysql
1.解壓縮文件包
cd /home/install
tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
2.復(fù)制解壓后的mysql目錄到系統(tǒng)的本地軟件目錄
mv mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql
3.刪除系統(tǒng)中原有的mysql用戶购笆,再添加系統(tǒng)mysql組和mysql用戶
userdel mysql
groupadd mysql
useradd -r -g mysql mysql
4.進入安裝mysql軟件目錄
cd /usr/local/mysql
5.修改當前目錄擁有者為mysql用戶
chown -R mysql:mysql ./
6.進入support-files路徑粗悯,發(fā)現(xiàn)沒有默認的配置文件my_default.cnf,從網(wǎng)上找一個
cd /usr/local/mysql/support-files
vi my_default.cnf
7.my_default.cnf內(nèi)容:
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 一般配置選項
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character-set-server=utf8
back_log = 300
max_connections = 3000
max_connect_errors = 50
table_open_cache = 4096
max_allowed_packet = 32M
#binlog_cache_size = 4M
max_heap_table_size = 128M
read_rnd_buffer_size = 16M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 16
query_cache_size = 128M
query_cache_limit = 4M
ft_min_word_len = 8
thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 128M
#log-bin=mysql-bin
long_query_time = 6
server_id=1
innodb_buffer_pool_size = 1G
innodb_thread_concurrency = 16
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = on
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
no-auto-rehash
default-character-set=utf8
safe-updates
[myisamchk]
key_buffer = 16M
sort_buffer_size = 16M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192
[client]
8.切換mysql用戶同欠,進入mysql路徑样傍,創(chuàng)建data數(shù)據(jù)文件夾,再進入bin路徑铺遂,安裝并初始化數(shù)據(jù)
su - mysql
cd /usr/local/mysql/bin/
mkdir data
./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
9.安裝信息
10.切換為root用戶衫哥,復(fù)制support-files路徑下的my_default.cnf到/etc路徑下,提示是否復(fù)蓋襟锐,輸入Y
su - root
cp -a my_default.cnf /etc/my.cnf
11.復(fù)制support-files路徑下的mysql.server撤逢,到/etc/init.d/
cp -a mysql.server /etc/init.d/mysqld
12.進入到mysql bin路徑下,查看當前是否有進程粮坞,如果有就kill掉蚊荣,然后啟動mysql服務(wù)
ps -A | grep mysql
/etc/init.d/mysqld start
13.連接mysql,進入mysql bin路徑莫杈,使用root用戶登陸互例,沒有密碼直接回車即可
./mysql -u root -p
14.修改數(shù)據(jù)庫可以遠程訪問
use mysql
SET SQL_SAFE_UPDATES = 0; ##這一句必須要有,如果不執(zhí)行這句數(shù)據(jù)庫處在安全模式筝闹,不可以使用非主鍵的字段做為條件更新數(shù)據(jù)敲霍。
update user set host = '%' where user = 'root';
15.重起mysql服務(wù)
/etc/init.d/mysqld restart
16.修改主數(shù)據(jù)庫192.168.200.199俊马,/etc/my.cnf配置文件,并且重起
server-id=199
log-bin=mysqlmaster-bin.log
sync_binlog=1
/etc/init.d/mysqld restart
17.修從主數(shù)據(jù)庫192.168.200.200肩杈,/etc/my.cnf配置文件柴我,并且重起
server-id=200
log-bin=mysqlslave-bin.log
sync_binlog=1
/etc/init.d/mysqld restart
18.在主數(shù)據(jù)庫上創(chuàng)建用于主從復(fù)制的賬戶
cd /usr/local/mysql/bin/
./mysql -uroot -p
msyql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.200.200' IDENTIFIED BY '111111';
19.在從數(shù)據(jù)庫服務(wù)器上設(shè)置主數(shù)據(jù)庫服務(wù)器向從數(shù)據(jù)庫服務(wù)器同步
mysql> change master to master_host = '192.168.200.199',master_user='repl',master_password='111111',master_log_file='mysqlmaster-bin.000002',master_log_pos=730;
master_log_file,master_log_pos是在主數(shù)據(jù)庫上執(zhí)行查詢得到的扩然,不要寫錯了艘儒。
show master status;
20.開啟從數(shù)據(jù)庫的復(fù)制線程。
mysql> start slave;
21.查看同步狀態(tài)
show slave status \G
22.測試一下夫偶,在主數(shù)據(jù)庫里插入一條數(shù)據(jù)試試界睁,看看重數(shù)據(jù)庫有沒有
在主庫上隨便建一個表,插入幾條數(shù)據(jù)兵拢,從數(shù)據(jù)可以同步過來翻斟。