1. 程式人生 > >MySQL遷移資料(切換資料儲存的資料夾)

MySQL遷移資料(切換資料儲存的資料夾)

備註:此流程是針對windows下相同版本(5.7.20)的(不同版本之間的遷移可參考,不一定成功,本人沒有測試)
1、首先做好資料備份,將原來的data資料夾(我的目錄D:\company\mysql-5.7.20-winx64\data)複製備份
2、停止原先的服務
進入到MySQL的bin資料夾(我的目錄D:\company\mysql-5.7.20-winx64\bin)
net stop mysql
3、如果與bin同級的目錄先存在data資料夾,刪除
4、執行初始化,這時會在bin同級目錄下產生data資料夾及資料夾內檔案

 mysqld --initialize

5、修改配置檔案my.ini中的屬性,datadir 為新的資料儲存目錄

 datadir = D:\company\mysql\data

6、將第四步中的data資料夾剪下到D:\company\mysql目錄下
7、將原來備份的data資料夾中的檔案複製到新的目錄下,其中紅框部分不需要複製,裡面的資料夾表示一個數據庫,其中ibdata1一定要複製,不然無法操作資料庫中的檔案
這裡寫圖片描述
8、啟動服務
net start mysql
如果未進行第6步,服務啟動失敗,服務沒有報告任何錯誤。
9、獲取初始化密碼

MySQL data目錄下 系統使用者名稱.err日誌檔案中,這個密碼是隨機的,可通過關鍵字搜尋
2018-05-16T02
:14:04.306268Z 1 [Note] A temporary password is generated for root@localhost: x9auqfBAr!u#

10、修改密碼

D:\company\mysql-5.7.20-winx64\bin>mysql -uroot -p
Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.20

Copyright (c) 2000
, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SET PASSWORD = PASSWORD('root'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql>