How to Set Up a MySQL Database on Your VPS
MySQL is a widely used database management system that allows you to store and manage data effectively. Follow these steps to set up MySQL on your VPS.
Step 1: Update Your VPS Packages
-
Log in to your VPS using SSH with your root or sudo user credentials:
-
Update your package lists to ensure you have the latest software:
Step 2: Install MySQL Server
-
Install the MySQL server package by running:
-
Once the installation is complete, ensure that the MySQL service is running:
Step 3: Secure Your MySQL Installation
-
Run the security script to enhance MySQL security:
-
Follow the prompts to:
- Set a strong root password.
- Remove anonymous users.
- Disallow remote root login (optional for security).
- Remove test databases.
- Reload privilege tables.
Step 4: Access the MySQL Shell
-
Log in to the MySQL shell using the root user:
-
Enter the root password you created during the secure installation step.
Step 5: Create a New MySQL Database
-
Inside the MySQL shell, create a new database with this command:
Replace
database_name
with your desired database name. -
Verify that the database was created:
Step 6: Create a New MySQL User
-
Create a new user and set a password:
Replace
username
with your desired username andpassword
with a strong password. -
Grant the new user privileges to the database:
-
Apply the changes:
Step 7: Test Your MySQL Database Connection
-
Exit the MySQL shell:
-
Test the new user’s access by logging in with the new credentials:
-
Once logged in, select the database:
-
Verify access by running a simple query:
Step 8: Set Up Remote Access (Optional)
-
If you need to allow remote access to the database, edit the MySQL configuration file:
-
Locate the line:
Change it to:
-
Restart MySQL to apply the changes:
-
Add a user with remote access privileges:
Step 9: Regular Maintenance
- Keep MySQL updated by running regular system updates.
- Monitor database performance and clean up unused data to optimize storage and efficiency.