歡迎您光臨本站 註冊首頁

MySQL數據庫主從同步實戰過程詳解

←手機掃碼閱讀     火星人 @ 2020-05-12 , reply:0

本文實例講述了MySQL數據庫主從同步實戰過程。分享給大家供大家參考,具體如下:
安裝環境說明
系統環境:
[[email protected]~]# cat /etc/redhat-release CentOS release 6.5 (Final)

[[email protected]~]# uname -r 2.6.32-431.el6.x86_64
數據庫:
由於是模擬環境,主從庫在同一臺服務器上,服務器IP地址192.168.1.7
主庫使用3306端口
從庫使用3307端口
數據庫數據目錄/data
安裝MySQL數據庫服務
下載軟件包
今天我們是用二進制安裝包進行佈署MySQL數據庫服務,其它方式的安裝佈署方法請參考前面的文章
[[email protected]~]#wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.51-linux2.6-x86_64.tar.gz
創建數據目錄、軟件安裝目錄
[[email protected]~]#mkdir /data{3306,3307} -p [[email protected]~]#mkdri /application
解壓軟件
[[email protected]~]#tar zxf mysql-5.5.51-linux2.6-x86_64.tar.gz [[email protected]~]#mv mysql-5.5.51-linux2.6-x86_64 /application/mysql-5.5.51 [[email protected]~]#ln -s /application/mysql-5.5.51 /application/mysql
創建用戶
[[email protected]~]#groupadd mysql [[email protected]~]#useradd -g mysql -M mysql
初始化數據庫
[[email protected]~]#/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql [[email protected]~]#/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
創建配置文件
[[email protected]~]#vi /data/3306/my.cnf [client] port = 3306 socket = /data/3306/mysql.sock [mysql] no-auto-rehash [mysqld] user = mysql port = 3306 socket = /data/3306/mysql.sock basedir = /application/mysql datadir = /data/3306/data open_files_limit = 1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_cache = 614 external-locking = FALSE max_allowed_packet =8M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 100 thread_concurrency = 2 query_cache_size = 2M query_cache_limit = 1M query_cache_min_res_unit = 2k thread_stack = 192K tmp_table_size = 2M max_heap_table_size = 2M long_query_time = 1 pid-file = /data/3306/mysql.pid log-bin = /data/3306/mysql-bin #主從同步的關鍵點,從庫上不需要開啟 relay-log = /data/3306/relay-bin relay-log-info-file = /data/3306/relay-log.info binlog_cache_size = 1M max_binlog_cache_size = 1M max_binlog_size = 2M expire_logs_days = 7 key_buffer_size = 16M read_buffer_size = 1M read_rnd_buffer_size = 1M bulk_insert_buffer_size = 1M lower_case_table_names = 1 skip-name-resolve slave-skip-errors = 1032,1062 replicate-ignore-db=mysql server-id = 1 #主庫從庫ID 不可相同 [mysqldump] quick max_allowed_packet = 2M [mysqld_safe] log-error=/data/3306/mysql3306.err pid-file=/data/3306/mysqld.pid
數據庫啟動腳本:
[[email protected]~]#vi /data/3306/mysql #!/bin/sh port=3306 user="root" pwd="123456" Path="/application/mysql/bin" sock="/data/${port}/mysql.sock" start_mysql() { if [ ! -e "$sock" ];then printf "Starting MySQL...
" /bin/sh ${Path}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null & else printf "MySQL is running...
" exit fi } stop_mysql() { if [ ! -e "$sock" ];then printf "MySQL is stopped...
" exit else printf "Stoping MySQL...
" ${Path}/mysqladmin -u ${user} -p${pwd} -S /data/${port}/mysql.sock shutdown fi } restart_mysql() { printf "Restarting MySQL...
" stop_mysql sleep 2 start_mysql } case $1 in start) start_mysql ;; stop) stop_mysql ;; restart) restart_mysql ;; *) printf "Usage: /data/${port}/mysql {start|stop|restart}
" esac
備註:主從庫配置文件與啟動文件一樣,只需修改端口與server-id即可完成配置
授權目錄並增加啟動文件可執行權限
[[email protected]~]#chown -R mysql.mysql /data [[email protected]~]#find /data -name mysql -exex chmod +x {} ;
啟動數據庫
[[email protected]~]#/data/3306/mysql start [[email protected]~]#/data/3307/mysql start
修改默認數據庫密碼
[[email protected]~]#mysqladmin -uroot password '123456' -S /data/3306/mysql.sock [[email protected]~]#mysqladmin -uroot password '123456' -S /data/3307/mysql.sock
測試登陸,可以登陸兩個數據庫即可完成全部安裝過程
配置主庫
1)備份主庫
mkdir /backup
登陸主庫創建用步同戶並授權
[[email protected]~]#mysql -uroot -p123456 -S /data/3306/mysql.sock mysql> grant replication slave on *.* to [email protected]'192.168.1.%' identified by'123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
執行鎖表操作
[[email protected]~]#/application/mysql/bin/mysql -uroot -p123456 -S /data/3306/mysql.sock -e "flush table with read lock;"
備份主庫
[[email protected]~]#/application/mysql/bin/mysql -uroot -p123456 -S /data/3306/mysql.sock -e "show master status;" >/backup/mysql.log [[email protected]~]#/application/mysql/bin/mysqldump -uroot -p123456 -S /data/3306/mysql.sock -A -B |gzip >/backup/mysql.sql.gz
解除鎖表狀態
[[email protected]~]#/application/mysql/bin/mysql -uroot -p123456 -S /data/3306/mysql.sock -e "unlock tables;"
備註:以上操作也可以登陸主庫進行,但是需要注意的是,執行鎖表操作後,需要另開啟一個窗口進行數據備份,不可直接退出,防止有數據寫入導致備份的數據不完整。最好是使用非交互式操作。
配置從庫實現主從同步
將主庫的備份文件解壓並恢復數據庫
[[email protected] ]#gzip -d mysql.sql.gz [[email protected] ]#/application/mysql/bin/mysql -uroot -p123456 -S /data/3307/mysql.sock < mysql.sql
查看LOG日誌
[[email protected] ]#cat mysql.log +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 424 | | | +------------------+----------+--------------+------------------+
登陸從庫執行下面的操作
mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.1.7', #服務器IP -> MASTER_PORT=3306, #主庫端口 -> MASTER_USER='rep', #同步的用戶 -> MASTER_PASSWORD='123456', #同步的用戶密碼 -> MASTER_LOG_FILE=' mysql-bin.000002', #binlog文件 -> MASTER_LOG_POS=424; #位置點 mysql> start slave; #開啟同步
等待60S後查看同步狀態
[[email protected] ]# mysql -S /data/3307/mysql.sock -e "show slave statusG"|egrep "Seconds_Behind_Master|_Running" Slave_IO_Running: Yes Slave_SQL_Running: Yes Seconds_Behind_Master: 0
只要出現上述情況說明主從同步成功
測試主從同步
主庫創建一個數據庫
[[email protected] ~]# mysql -S /data/3306/mysql.sock -e "create database tongbuku" [[email protected] ~]# mysql -S /data/3306/mysql.sock -e "show databases" 

+-----------------------------+ | Database | +-----------------------------+ | information_schema | | mysql | | performance_schema | | test | | tongbuku | +-----------------------------+
查看從庫同步情況
[[email protected] ~]# mysql -S /data/3307/mysql.sock -e "show databases" 

+-----------------------------+ | Database | +-----------------------------+ | information_schema | | mysql | | performance_schema | | test | | tongbuku | 

+-----------------------------+
表明主從同步狀態正常,也可以在主庫新的數據表中創建表,再插入新的數據來測試主從同步狀態
 


[火星人 ] MySQL數據庫主從同步實戰過程詳解已經有148次圍觀

http://coctec.com/docs/mysql/show-post-234050.html