How to move MySQL database files from C drive to D drive on Windows 10

Preface

When installing and using MySQL, MySQL will be installed on the C drive by default, and its database files will also be on the C drive by default. Generally, we use the C drive as the system drive. If the database file is stored on the C drive, along with the database As the data in the C drive becomes larger and larger, the C drive space will become less and less. To this end, the MySQL database files need to be migrated from the C drive to other drives. The specific steps are as follows.

View the current MySQL database file path

Open Navicat, connect to the local mysql database, click the "Query" menu in the menu bar, click "New Query", enter show variables like 'datadir';and click Run to execute the statement, you can see the storage path of the file in the execution result
Insert image description here

Stop MySQL service

On Windows, stop the MySQL service through the Services application or the command line.
Enter "service" in the search box on the Windows taskbar to open the service window.
Insert image description here
Find MySQL80 in the service, right-click the mouse and select "Stop"
Insert image description here

Copy the MySQL database file from C drive to D drive

Create a folder for database storage on the D drive. According to the database storage path on the C drive: "C:\ProgramData\MySQL\MySQL Server 8.0\Data\", create a ProgramData folder on the D drive and create a MySQL file under this folder. folder, create the MySQL Server 8.0 folder under the MySQL folder, and copy the Data file in the corresponding folder on the C drive to the "D:\ProgramData\MySQL\MySQL Server 8.0" folder
Insert image description here

Modify MySQL configuration file

Find the my.ini file in the "C:\ProgramData\MySQL\MySQL Server 8.0" folder, right-click and select "Open with code". Open it with vscode.
Insert image description here
After opening the my.ini file in vscode, use the Ctrl+F shortcut Press the key to bring up the search box, enter datadir to find the line of code, usually on line 95.
Insert image description here
After finding the line, change the C in the line of code to D. No other modifications are needed.
Insert image description here

Restart the service and verify whether it is successful

After the modification is completed, restart the MySQL80 service in the service. If the service starts successfully, it means that the above modification is correct.
Open Navicat and re-execute the command in Navicat show variables like 'datadir';. You can see that the file path has been modified to the D drive.
Insert image description here
In order to verify whether the database file is Correct, we cut the Data folder under the "C:\ProgramData\MySQL\MySQL Server 8.0" folder on the C drive to the desktop, close Navicat, restart the MySQL80 service, then reopen Navicat, open the database table, the file is not Affected. The database file migration was successful.

Note that there is a pitfall here . When modifying the my.ini file, never open it with Notepad. The blog articles I found on the Internet all say to use Notepad to open it. When saving after modification, select ANSI to save in the encoding. I After installing this operation, restarting the service will report an error, causing the MySQL80 service to fail to open. I use vscode to open and modify without error. It is recommended to use a dedicated ini editor to open it. My operating system does not have an ini editor installed, so I use vscode to open and edit, and I find that it can be modified normally.

In addition: Before modifying the my.ini file, it is recommended to copy the my.ini file to another place to prevent irreparable losses caused by errors during the modification process.

Guess you like

Origin blog.csdn.net/w137160164/article/details/134847754