Three Methods to Connect MySQL Database Locally (localhost)

Publish: 2017-03-20 | Modify: 2017-06-21

How to Connect to MySQL Database Using localhost

localhost

Using Web Tools

You can install web tools such as PHPMyadmin and Adminer on the server to manage the database. These tools support both local (localhost) and remote connections and are very convenient and flexible to use. Currently, LNMP and OneinStack have integrated PHPMyadmin management tools.

However, installing these web tools means that others can also access PHPMyadmin from the network. To avoid situations like brute-force attacks, it is recommended to use htpasswd authentication with Nginx to add an additional layer of protection.

phpmyadmin

Command Line Management

If you are more experienced and do not want to use web tools, you can use the command line to manage the database. This method requires familiarity with SQL statements; otherwise, it may be difficult to operate. Execute the following commands on the server to connect to the database:

# Connect to the database
mysql -u username -p password
# Show databases
show databases;
# Use the 'test' database
use test;
# Show tables
show tables;
# ...other operations

Navicat Client Tool

Navicat is an excellent client management tool that supports connections to various databases. You may wonder, didn't we say we want to connect to the database locally (localhost)? How can Navicat achieve remote connections? That's because Navicat is very powerful and can achieve localhost connections to MySQL databases through an SSH tunnel. Here are the steps:

  1. Open Navicat tool.
  2. Create a new MySQL connection.
  3. Switch to the SSH tab and fill in your server account information.
  4. Switch back to the General tab and fill in your MySQL information.
  5. Now you can connect to the database on your local computer.

Navicat SSH Navicat General Navicat Connection Diagram

Conclusion

PHPMyadmin is relatively risky because it is managed via the web and does not support importing large database files. The command line method is more complicated. A more convenient option would be to use client tools like Navicat. These are some common methods for connecting to a local database. If you have a better solution, please leave a comment below and let's learn together.


Comments