国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

MySQL數(shù)據(jù)庫(kù)主從同步實(shí)戰(zhàn)過程詳解

瀏覽:9日期:2023-10-15 10:10:58

本文實(shí)例講述了MySQL數(shù)據(jù)庫(kù)主從同步實(shí)戰(zhàn)過程。分享給大家供大家參考,具體如下:

接上一篇:MySQL數(shù)據(jù)庫(kù)入門之備份數(shù)據(jù)庫(kù)

安裝環(huán)境說明

系統(tǒng)環(huán)境:

[root@~]# cat /etc/redhat-release CentOS release 6.5 (Final)[root@~]# uname -r2.6.32-431.el6.x86_64

數(shù)據(jù)庫(kù):

由于是模擬環(huán)境,主從庫(kù)在同一臺(tái)服務(wù)器上,服務(wù)器IP地址192.168.1.7

主庫(kù)使用3306端口 從庫(kù)使用3307端口 數(shù)據(jù)庫(kù)數(shù)據(jù)目錄/data安裝MySQL數(shù)據(jù)庫(kù)服務(wù)

下載軟件包

今天我們是用二進(jìn)制安裝包進(jìn)行布署MySQL數(shù)據(jù)庫(kù)服務(wù),其它方式的安裝布署方法請(qǐng)參考前面的文章

[root@~]#wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.51-linux2.6-x86_64.tar.gz

創(chuàng)建數(shù)據(jù)目錄、軟件安裝目錄

[root@~]#mkdir /data{3306,3307} -p[root@~]#mkdri /application

解壓軟件

[root@~]#tar zxf mysql-5.5.51-linux2.6-x86_64.tar.gz [root@~]#mv mysql-5.5.51-linux2.6-x86_64 /application/mysql-5.5.51[root@~]#ln -s /application/mysql-5.5.51 /application/mysql

創(chuàng)建用戶

[root@~]#groupadd mysql[root@~]#useradd -g mysql -M mysql

初始化數(shù)據(jù)庫(kù)

[root@~]#/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql[root@~]#/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql

創(chuàng)建配置文件

[root@~]#vi /data/3306/my.cnf[client]port = 3306socket = /data/3306/mysql.sock[mysql]no-auto-rehash[mysqld]user = mysqlport = 3306socket = /data/3306/mysql.sockbasedir = /application/mysqldatadir = /data/3306/dataopen_files_limit = 1024back_log = 600max_connections = 800max_connect_errors = 3000table_cache = 614external-locking = FALSEmax_allowed_packet =8Msort_buffer_size = 1Mjoin_buffer_size = 1Mthread_cache_size = 100thread_concurrency = 2query_cache_size = 2Mquery_cache_limit = 1Mquery_cache_min_res_unit = 2kthread_stack = 192Ktmp_table_size = 2Mmax_heap_table_size = 2Mlong_query_time = 1pid-file = /data/3306/mysql.pidlog-bin = /data/3306/mysql-bin#主從同步的關(guān)鍵點(diǎn),從庫(kù)上不需要開啟relay-log = /data/3306/relay-binrelay-log-info-file = /data/3306/relay-log.infobinlog_cache_size = 1Mmax_binlog_cache_size = 1Mmax_binlog_size = 2Mexpire_logs_days = 7key_buffer_size = 16Mread_buffer_size = 1Mread_rnd_buffer_size = 1Mbulk_insert_buffer_size = 1Mlower_case_table_names = 1skip-name-resolveslave-skip-errors = 1032,1062replicate-ignore-db=mysqlserver-id = 1 #主庫(kù)從庫(kù)ID 不可相同[mysqldump]quickmax_allowed_packet = 2M[mysqld_safe]log-error=/data/3306/mysql3306.errpid-file=/data/3306/mysqld.pid

數(shù)據(jù)庫(kù)啟動(dòng)腳本:

[root@~]#vi /data/3306/mysql#!/bin/shport=3306user='root'pwd='123456'Path='/application/mysql/bin'sock='/data/${port}/mysql.sock'start_mysql(){ if [ ! -e '$sock' ];then printf 'Starting MySQL...n' /bin/sh ${Path}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null & else printf 'MySQL is running...n' exit fi}stop_mysql(){ if [ ! -e '$sock' ];then printf 'MySQL is stopped...n' exit else printf 'Stoping MySQL...n' ${Path}/mysqladmin -u ${user} -p${pwd} -S /data/${port}/mysql.sock shutdown fi}restart_mysql(){ printf 'Restarting MySQL...n' stop_mysql sleep 2 start_mysql}case $1 instart) start_mysql;;stop) stop_mysql;;restart) restart_mysql;;*) printf 'Usage: /data/${port}/mysql {start|stop|restart}n'esac

備注:主從庫(kù)配置文件與啟動(dòng)文件一樣,只需修改端口與server-id即可完成配置

授權(quán)目錄并增加啟動(dòng)文件可執(zhí)行權(quán)限

[root@~]#chown -R mysql.mysql /data[root@~]#find /data -name mysql -exex chmod +x {} ;

啟動(dòng)數(shù)據(jù)庫(kù)

[root@~]#/data/3306/mysql start[root@~]#/data/3307/mysql start

修改默認(rèn)數(shù)據(jù)庫(kù)密碼

[root@~]#mysqladmin -uroot password ’123456’ -S /data/3306/mysql.sock[root@~]#mysqladmin -uroot password ’123456’ -S /data/3307/mysql.sock

測(cè)試登陸,可以登陸兩個(gè)數(shù)據(jù)庫(kù)即可完成全部安裝過程

配置主庫(kù)

1)備份主庫(kù)

mkdir /backup

登陸主庫(kù)創(chuàng)建用步同戶并授權(quán)

[root@~]#mysql -uroot -p123456 -S /data/3306/mysql.sockmysql> grant replication slave on *.* to rep@’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)

執(zhí)行鎖表操作

[root@~]#/application/mysql/bin/mysql -uroot -p123456 -S /data/3306/mysql.sock -e 'flush table with read lock;'

備份主庫(kù)

[root@~]#/application/mysql/bin/mysql -uroot -p123456 -S /data/3306/mysql.sock -e 'show master status;' >/backup/mysql.log[root@~]#/application/mysql/bin/mysqldump -uroot -p123456 -S /data/3306/mysql.sock -A -B |gzip >/backup/mysql.sql.gz

解除鎖表狀態(tài)

[root@~]#/application/mysql/bin/mysql -uroot -p123456 -S /data/3306/mysql.sock -e 'unlock tables;'

備注:以上操作也可以登陸主庫(kù)進(jìn)行,但是需要注意的是,執(zhí)行鎖表操作后,需要另開啟一個(gè)窗口進(jìn)行數(shù)據(jù)備份,不可直接退出,防止有數(shù)據(jù)寫入導(dǎo)致備份的數(shù)據(jù)不完整。最好是使用非交互式操作。

配置從庫(kù)實(shí)現(xiàn)主從同步

將主庫(kù)的備份文件解壓并恢復(fù)數(shù)據(jù)庫(kù)

[root@backup ]#gzip -d mysql.sql.gz[root@backup ]#/application/mysql/bin/mysql -uroot -p123456 -S /data/3307/mysql.sock < mysql.sql

查看LOG日志

[root@backup ]#cat mysql.log+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000002 | 424 | | |+------------------+----------+--------------+------------------+

登陸從庫(kù)執(zhí)行下面的操作

mysql> CHANGE MASTER TO -> MASTER_HOST=’192.168.1.7’, #服務(wù)器IP -> MASTER_PORT=3306, #主庫(kù)端口 -> MASTER_USER=’rep’, #同步的用戶 -> MASTER_PASSWORD=’123456’, #同步的用戶密碼 -> MASTER_LOG_FILE=’ mysql-bin.000002’, #binlog文件 -> MASTER_LOG_POS=424; #位置點(diǎn)mysql> start slave; #開啟同步

等待60S后查看同步狀態(tài)

[root@backup ]# 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

只要出現(xiàn)上述情況說明主從同步成功

測(cè)試主從同步

主庫(kù)創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)

[root@backup ~]# mysql -S /data/3306/mysql.sock -e 'create database tongbuku'[root@backup ~]# mysql -S /data/3306/mysql.sock -e 'show databases'+-----------------------------+| Database |+-----------------------------+| information_schema || mysql || performance_schema || test || tongbuku |+-----------------------------+

查看從庫(kù)同步情況

[root@backup ~]# mysql -S /data/3307/mysql.sock -e 'show databases'+-----------------------------+| Database |+-----------------------------+| information_schema || mysql || performance_schema || test || tongbuku |+-----------------------------+

表明主從同步狀態(tài)正常,也可以在主庫(kù)新的數(shù)據(jù)表中創(chuàng)建表,再插入新的數(shù)據(jù)來測(cè)試主從同步狀態(tài)

更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過程技巧大全》及《MySQL數(shù)據(jù)庫(kù)鎖相關(guān)技巧匯總》

希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。

標(biāo)簽: MySQL 數(shù)據(jù)庫(kù)
相關(guān)文章:
主站蜘蛛池模板: 达尔| 法库县| 上高县| 磐安县| 阿合奇县| 菏泽市| 洪雅县| 华蓥市| 绩溪县| 神池县| 定安县| 绵阳市| 介休市| 个旧市| 台南市| 垦利县| 鲜城| 张家港市| 怀化市| 南雄市| 聂荣县| 浏阳市| 景泰县| 天长市| 苏尼特左旗| 那坡县| 开江县| 靖江市| 长垣县| 金溪县| 庄河市| 县级市| 宁化县| 噶尔县| 临城县| 荥阳市| 留坝县| 舒兰市| 江门市| 林州市| 鹤山市|