《MySQL主從復制》要點:
本文介紹了MySQL主從復制,希望對您有用。如果有疑問,可以聯系我們。
一、簡述原理:
原理圖
1、master記載二進制日志
2、slave的I/O線程讀取master的二進制日志,并將其寫入到中繼日志中,SQL線程從中繼日志中讀取時間,并重放其中變亂,更新slave的數據
二、準備工作:
封閉防火墻
#server iptables stop
封閉開機自啟
#chkconfig iptables off
封閉selinux
#setenforce 0
在/etc/selinux/config 中,將SELINUX=enforcing改為SELINUX=disabled
同步光陰
#ntpdate 202.120.2.101
=======================================================
三、安裝mysql5.6
#cd /usr/local/src
解壓mysql包
#tar -zxf MySQL-5.6.23-1.el6.x86_64.rpm-bundle.tar
用yum安裝mysql,辦理依賴關系
#yum install MySQL-shared-compat-5.6.23-1.el6.x86_64.rpm
#yum install MySQL-server-5.6.23-1.el6.x86_64.rpm
#yum install MySQL-client-5.6.23-1.el6.x86_64.rpm
#yum install MySQL-devel-5.6.23-1.el6.x86_64.rpm
#yum install MySQL-shared-5.6.23-1.el6.x86_64.rpm
創立數據目錄
#mkdir -pv /home/mydata/data
#chown -R mysql.mysql /home//mydata
#chmod -R +w /home/mydata
四、改動配置文件
配置文件【主】
#cat /etc/my.cnf
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
datadir = /home/mydata/data
port = 3306
socket = /var/lib/mysql/mysql.sock
log-bin = master-bin
server_id = 1
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
pid-file = /home/mydata/data/mysql.pid
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
innodb_file_per_table = on
thread_concurrency = 8
skip_name_resolve = on
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
配置文件【從】
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
datadir = /home/mydata/data
port = 3306
server_id = 2
pid-file = /home/mydata/data/mysql.pid
relay-log = relay-bin
socket = /var/lib/mysql/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
innodb_file_per_table = on
thread_concurrency = 8
skip_name_resolve = on
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
初始化MySQL
#/usr/bin/mysql_install_db --datadir=/usr/local/work/mydata --user=mysql
啟動服務
#service mysql start
設置root暗碼
#mysql>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
【主節點】授權復制權限賬號給從節點
#mysql>grant replication client,replication slave on *.* to 'repuser'@'192.168.%.%' identified by 'reppasswd';
#mysql>flush privileges;
查看狀態
#mysql>show master status;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
【從節點】指定主節點,復制賬號
#mysql>change master to master_host='192.168.1.6',master_user='repuser',master_password='reppasswd',master_log_file='master-bin.000001',master_log_pos=120;
查看狀態
#mysql>show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
備注:這里只講到mysql主從復制,但沒有講到如何實現高可用,可以應用keepalived來實現.
歡迎參與《MySQL主從復制》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/7136.html