While building a website, you may often need to upload data or import/export databases. Today, I needed to import a database during my web development process, so I’m sharing this note in hopes it will help others who encounter the same task.
In this example, I’ll demonstrate how to import a MySQL database on a Linux server running CentOS 7.9 with an LNMP environment.
- Log in to the Server
You can use SSH tools such as Xshell to connect to your server. (You can find and download it from websites like Baidu or CSDN.)
- Create and Import a Database
If you’re importing a new database, you’ll first need to create one. Below are the steps to log in to MySQL, create a new database, and import data.
1) Log in to MySQL
mysql -uroot -p
Enter your root password when prompted, then press Enter.
2) Create a New Database
It’s best to choose a database name that’s easy to remember, such as your website domain.
For example:
create database www.wativate.com;
3) Select the Database
use www.wativate.com;
4) Set the Character Encoding
set names utf8mb4;
5) Import the Database
First, upload your SQL file (e.g., 20251006.sql) to your server—commonly to the /home directory.
You can use tools like WinSCP to transfer files from your computer to the server.
Then run the import command:
source /home/20251006.sql;
6) Exit MySQL
exit;
Notes
Always double-check that you’ve selected the correct database before importing, to avoid overwriting or importing data into the wrong one.
Once the import is complete, you can verify your tables and data using show tables;.
Top comments (0)