同步云數(shù)據(jù)庫(kù)MySQL到自建數(shù)據(jù)庫(kù)

摘要:?同步云數(shù)據(jù)庫(kù)MySQL到自建數(shù)據(jù)庫(kù)

備份恢復(fù)到本地的相關(guān)說(shuō)明,請(qǐng)參考https://help.aliyun.com/knowledge_detail/41817.html

本例環(huán)境為 Mysql5.6?

阿里云 RDS / Mysql 5.6

本地環(huán)境Ubuntu 14.04 / Mysql 5.6

1.安裝Percona Xtrabackup

wget?https://repo.percona.com/apt/percona-release_0.1-5.(lsb_release?-sc)_all.deb

dpkg -i percona-release_0.1-5.$(lsb_release -sc)_all.deb

apt-get update

apt-get install percona-xtrabackup-24

2.安裝Mysql服務(wù)和客戶端

apt-get install mysql-server-5.6 mysql-client-5.6

/etc/init.d/mysql stop

3.下載數(shù)據(jù)庫(kù)備份文件

wget -c '<數(shù)據(jù)備份文件外網(wǎng)下載地址>' -O <自定義文件名>.tar.gz

4.解壓備份文件

bash rds_backup_extract.sh -f <數(shù)據(jù)備份文件名>.tar.gz -C /home/mysql/data

5.恢復(fù)解壓好的文件

innobackupex --defaults-file=/home/mysql/data/backup-my.cnf --apply-log /home/mysql/data

6.修改backup-my.cnf參數(shù)

vim /home/mysql/data/backup-my.cnf

# This MySQL options file was generated by >>innobackupex.

# The MySQL server

[mysqld]

innodb_checksum_algorithm=innodb

#innodb_log_checksum_algorithm=innodb

innodb_data_file_path=ibdata1:200M:autoextend

innodb_log_files_in_group=2

innodb_log_file_size=1048576000

#innodb_fast_checksum=false

innodb_page_size=16384

#innodb_log_block_size=512

innodb_undo_directory=.

innodb_undo_tablespaces=0

#rds_encrypt_data=false

#innodb_encrypt_algorithm=aes_128_ecb

7.修改文件屬主

chown -R mysql:mysql /home/mysql/data

8.啟動(dòng)MySQL進(jìn)程

mysqld_safe --defaults-file=/home/mysql/data/backup-my.cnf --user=mysql --datadir=/home/mysql/data

9.登錄MySQL數(shù)據(jù)庫(kù)進(jìn)行修改

mysql -uroot

mysql>delete from mysql.db where user<>'root' and char_length(user)>0;delete from mysql.tables_priv where user<>'root' and char_length(user)>0;flush privileges;

mysql>use mysql;

mysql>drop table slave_master_info;

mysql>drop table slave_relay_log_info;

mysql>drop table slave_worker_info;

mysql>drop table innodb_index_stats;

mysql>drop table innodb_table_stats;

mysql>source /usr/share/mysql/mysql_system_tables.sql

mysql>quit

10.修改my.cnf(把backup-my.cnf參數(shù)復(fù)制到my.cnf)

mysqladmin shutdown

vim /etc/mysql/my.cnf

[client]

port = 3306

socket = /var/run/mysqld/mysqld.sock

[mysqld_safe]

socket = /var/run/mysqld/mysqld.sock

nice = 0

[mysqld]

user = mysql

pid-file = /var/run/mysqld/mysqld.pid

socket = /var/run/mysqld/mysqld.sock

port = 3306

basedir = /usr

datadir = /home/mysql/data

tmpdir = /tmp

lc-messages-dir = /usr/share/mysql

skip-external-locking

myisam-recover = BACKUP

log_error = /var/log/mysql/error.log

#阿里云RDS優(yōu)化配置

auto_increment_increment = 1

auto_increment_offset = 1

back_log = 3000

binlog_cache_size = 1M

binlog_checksum = CRC32

binlog_row_image = full

binlog_stmt_cache_size = 32768

character_set_server = utf8

concurrent_insert = 1

connect_timeout = 10

default_storage_engine = InnoDB

default_time_zone = SYSTEM

default_week_format = 0

delayed_insert_limit = 100

delayed_insert_timeout = 300

delayed_queue_size = 1000

delay_key_write = ON

div_precision_increment = 4

eq_range_index_dive_limit = 10

explicit_defaults_for_timestamp = false

ft_min_word_len = 4

ft_query_expansion_limit = 20

group_concat_max_len = 1024

innodb_adaptive_hash_index = ON

innodb_additional_mem_pool_size = 2097152

innodb_autoinc_lock_mode = 1

innodb_concurrency_tickets = 500

innodb_ft_max_token_size = 84

innodb_ft_min_token_size = 3

innodb_large_prefix = 0

innodb_lock_wait_timeout = 50

innodb_max_dirty_pages_pct = 75

innodb_old_blocks_pct = 37

innodb_old_blocks_time = 0

innodb_online_alter_log_max_size = 134217728

innodb_open_files = 300

innodb_print_all_deadlocks = OFF

innodb_purge_batch_size = 20

innodb_purge_threads = 1

innodb_read_ahead_threshold = 56

innodb_read_io_threads = 4

innodb_rollback_on_timeout = OFF

innodb_stats_method = nulls_equal

innodb_stats_on_metadata = OFF

innodb_stats_sample_pages = 8

innodb_strict_mode = OFF

innodb_table_locks = ON

innodb_thread_concurrency = 0

innodb_thread_sleep_delay = 10000

innodb_write_io_threads = 4

interactive_timeout = 7200

key_cache_age_threshold = 300

key_cache_block_size = 1024

key_cache_division_limit = 100

log_queries_not_using_indexes = OFF

long_query_time = 1

#loose_max_statement_time = 0

#loose_rds_indexstat = OFF

#loose_rds_max_tmp_disk_space = 10737418240

#loose_rds_tablestat = ON

#loose_rds_threads_running_high_watermark = 50000

#loose_tokudb_buffer_pool_ratio = 0

lower_case_table_names = 1

low_priority_updates = 0

max_allowed_packet = 1024M

max_connect_errors = 20

max_length_for_sort_data = 1024

max_prepared_stmt_count = 16382

max_write_lock_count = 102400

myisam_sort_buffer_size = 262144

net_read_timeout = 30

net_retry_count = 10

net_write_timeout = 60

open_files_limit = 65535

performance_schema = OFF

query_alloc_block_size = 8192

query_cache_limit = 1048576

query_cache_size = 0

query_cache_type = 1

query_cache_wlock_invalidate = OFF

query_prealloc_size = 8192

#rds_reset_all_filter = 0

slow_launch_time = 2

sql_mode =

table_definition_cache = 512

table_open_cache = 2000

thread_stack = 262144

tmp_table_size = 262144

transaction_isolation = READ-COMMITTED

wait_timeout = 86400

#優(yōu)化結(jié)束

#GTID設(shè)置

server-id = 148

log-bin = mysql.bin

log-bin-index = mysql-bin.index

log-slave-updates = 1

skip_slave_start = 1

relay-log = relay-log

relay_log_index = relay-log.index

expire_logs_days = 0

max_binlog_size = 500M

default-storage-engine=INNODB

master-info-repository=TABLE

relay-log-info_repository=TABLE

binlog-format=ROW

gtid-mode=on

enforce-gtid-consistency=true

#backup-my.cnf參數(shù)

innodb_checksum_algorithm=innodb

#innodb_log_checksum_algorithm=innodb

innodb_data_file_path=ibdata1:200M:autoextend

innodb_log_files_in_group=2

innodb_log_file_size=1048576000

#innodb_fast_checksum=false

innodb_page_size=16384

#innodb_log_block_size=512

innodb_undo_directory=.

innodb_undo_tablespaces=0

#backup-my.cnf結(jié)束

replicate-ignore-db=mysql

replicate-ignore-db=test

replicate-ignore-db=information_schema

replicate-ignore-db=performance_schema

replicate-do-db=db1

replicate-do-db=db2

#GTID結(jié)束

[mysqldump]

quick

quote-names

max_allowed_packet = 16M

[mysql]

[isamchk]

key_buffer = 16M

!includedir /etc/mysql/conf.d/

注:my.cnf的參數(shù)可以參考RDS的參數(shù)兄渺,我這里是照搬蜡秽,請(qǐng)自己對(duì)照情況進(jìn)行修改。

11.設(shè)置slave(請(qǐng)先在rds控制臺(tái)創(chuàng)建一個(gè)用來(lái)同步的賬戶,建議只讀)

/etc/init.d/mysql/restart

cat /home/data/mysql/xtrabackup_slave_info

#文件里面就兩段字,復(fù)制下來(lái)帘睦,待會(huì)用到喉磁。

mysql -uroot

mysql>SET GLOBAL gtid_purged='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:1-123456';

mysql>CHANGE MASTER TO MASTER_HOST='RDS外網(wǎng)地址', MASTER_PORT=3306, MASTER_USER='同步賬號(hào)', MASTER_PASSWORD='同步密碼', MASTER_AUTO_POSITION=1;

mysql>START SLAVE;

mysql>SHOW SLAVE STATUS G

問(wèn)題解答

1.首次啟動(dòng)數(shù)據(jù)庫(kù)出現(xiàn)如下提示

[ERROR] InnoDB: ./ibdata1 can't be opened in read-write mode

[ERROR] InnoDB: The system tablespace must be writable!

[ERROR] Plugin 'InnoDB' init function returned error.

[ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.

[ERROR] Unknown/unsupported storage engine: InnoDB

[ERROR] Aborting

PS:重啟服務(wù)器即可,刪除ib*什么的不管用官脓。

2.unknown variable 'xxxx'

[ERROR]/usr/sbin/mysqld: unknown variable 'xxxx'

PS:到my.cnf里面注釋xxxx

3.Table './mysql/xxx' 報(bào)錯(cuò)

[ERROR] /usr/sbin/mysqld: Table './mysql/db' is marked as crashed and should be repaired

[Warning] Checking table: './mysql/db'

[ERROR] 1 client is using or hasn't closed the table properly

[ERROR] /usr/sbin/mysqld: Table './mysql/event' is marked as crashed and should be repaired

[Warning] Checking table: './mysql/event'

[ERROR] 1 client is using or hasn't closed the table properly

PS:使用myisamchk -c -r /home/mysql/data/db/tablesname.MYI修復(fù)即可

4.information that should help you find out what is causing the crash.

It is possible that mysqld could use up to

key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 68104 K bytes of memory

Hope that's ok; if not, decrease some variables in the equation.

Thread pointer: 0xxxxxxxxxxxxx

Attempting backtrace. You can use the following information to find out

where mysqld died. If you see no messages after this, something went

terribly wrong...

PS:...他只是卡住了而已,my.cnf里面部分參數(shù)設(shè)置不當(dāng)涝焙,等一會(huì)就可以連了卑笨。。別問(wèn)我為什么知道仑撞。赤兴。

5.同步時(shí)報(bào)1236錯(cuò)誤

[ERROR] Error reading packet from server: The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires. (server_errno=1236)

ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.', Error_code: 1236

[ERROR] Error reading packet from server: The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires. (server_errno=1236)

PS:重新從RDS獲取新的備份(當(dāng)前的新備份)

最后,本地my.cnf里面的配置隧哮,如果你不知道有些參數(shù)數(shù)值應(yīng)該設(shè)置多少桶良,可以登陸RDS服務(wù)器使用show命令進(jìn)行查詢,查詢到的數(shù)值單位是字節(jié)沮翔,不會(huì)換算自己百度用工具換算一下就行陨帆,上文中關(guān)于my.cnf阿里云優(yōu)化的部分,全部使用了RDS控制臺(tái)里面的參數(shù)(導(dǎo)出復(fù)制進(jìn)去就行采蚀,記得注釋掉有rds的參數(shù))疲牵,RDS里面沒(méi)有的參數(shù),你本地可以直接注釋掉榆鼠。

對(duì)于GTID的參數(shù)纲爸,官方有很詳細(xì)的解釋,這里就不多做解釋了妆够。

以上识啦,遇到問(wèn)題歡迎留言。

版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻(xiàn)神妹,版權(quán)歸作者所有颓哮,本社區(qū)不擁有所有權(quán),也不承擔(dān)相關(guān)法律責(zé)任鸵荠。如果您發(fā)現(xiàn)本社區(qū)中有涉嫌抄襲的內(nèi)容题翻,歡迎發(fā)送郵件至:yqgroup@service.aliyun.com?進(jìn)行舉報(bào),并提供相關(guān)證據(jù)腰鬼,一經(jīng)查實(shí)嵌赠,本社區(qū)將立刻刪除涉嫌侵權(quán)內(nèi)容。

原文鏈接

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末熄赡,一起剝皮案震驚了整個(gè)濱河市姜挺,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌彼硫,老刑警劉巖炊豪,帶你破解...
    沈念sama閱讀 212,718評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件凌箕,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡词渤,警方通過(guò)查閱死者的電腦和手機(jī)牵舱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)缺虐,“玉大人芜壁,你說(shuō)我怎么就攤上這事「叩” “怎么了慧妄?”我有些...
    開(kāi)封第一講書(shū)人閱讀 158,207評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)剪芍。 經(jīng)常有香客問(wèn)我塞淹,道長(zhǎng),這世上最難降的妖魔是什么罪裹? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,755評(píng)論 1 284
  • 正文 為了忘掉前任饱普,我火速辦了婚禮,結(jié)果婚禮上状共,老公的妹妹穿的比我還像新娘费彼。我一直安慰自己,他們只是感情好口芍,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布箍铲。 她就那樣靜靜地躺著,像睡著了一般鬓椭。 火紅的嫁衣襯著肌膚如雪颠猴。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 50,050評(píng)論 1 291
  • 那天小染,我揣著相機(jī)與錄音翘瓮,去河邊找鬼。 笑死裤翩,一個(gè)胖子當(dāng)著我的面吹牛资盅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播踊赠,決...
    沈念sama閱讀 39,136評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼呵扛,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了筐带?” 一聲冷哼從身側(cè)響起今穿,我...
    開(kāi)封第一講書(shū)人閱讀 37,882評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎伦籍,沒(méi)想到半個(gè)月后蓝晒,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體腮出,經(jīng)...
    沈念sama閱讀 44,330評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評(píng)論 2 327
  • 正文 我和宋清朗相戀三年芝薇,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了胚嘲。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,789評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡洛二,死狀恐怖馋劈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情灭红,我是刑警寧澤,帶...
    沈念sama閱讀 34,477評(píng)論 4 333
  • 正文 年R本政府宣布口注,位于F島的核電站变擒,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏寝志。R本人自食惡果不足惜娇斑,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評(píng)論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望材部。 院中可真熱鬧毫缆,春花似錦、人聲如沸乐导。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,864評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)物臂。三九已至旺拉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間棵磷,已是汗流浹背蛾狗。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,099評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留仪媒,地道東北人沉桌。 一個(gè)月前我還...
    沈念sama閱讀 46,598評(píng)論 2 362
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像算吩,于是被迫代替她去往敵國(guó)和親留凭。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容