How To Connect to MySQL Server?
In this tutorial, we will introduce several tools for connecting to MySQL server.
After you have installed the MySQL server, you can use any client program to connect to the MySQL server, such as mysql command-line client or some GUI tools: MySQL Workbench, phpMyAdmin, HeidiSQL and so on.
If you want to list databases or tables, query data or do anything on MySQL server, you must connect to the server first.
Mysql Command-Line Client
The mysql command-line client mysql
is a simple SQL shell. You can use it to connect to server, query data from table or maintain data.
The mysql command-line client is installed by default normally. You can find it in the bin directory MySQL installation folder, such as: c:\Program Files\MySQL\MySQL Server 8.0\bin
on Windows platform. It is a good idea adding the bin directory path to the PATH
environment variable,and you can use mysql easily.
On Linux or MacOS platform, you can use mysql everywhere, since it is in the PATH
environment variable after installation.
Follow the steps below to connect to the MySQL server using the mysql command line client:
-
Open the CMD on windows or terminal on Linux/MacOS. Use the following command to navigate to the bin directory of the MySQL server if it is not in the
PATH
, or else skip this step.cd c:\Program Files\MySQL\MySQL Server 8.0\bin
The location of bin directory may be different on your computer.
-
Use the following command to connect to the MySQL server:
mysql -u root -p
Here,
-u root
means that you useroot
account to connect to MySQL server.Here, the command is used to connect the port
3306
in thelocalhost
machine. It is default. If you want to connect to another port in another machine, you should use the-h
and-P
options in the command, likemysql -u root -p -h 192.168.1.100 -P 3307
.Then follow the prompts to enter the
root
account’s password, and press Enter. After the verification is passed, you will see the following output:Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 27 Server version: 8.0.26 Homebrew Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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>
-
Use
SHOW DATABASES
to display all the Databases on the MySQL Server:mysql> show databases;
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sakila | | sys | | world | +--------------------+ 6 rows in set (0.00 sec)
So far, successfully connected to the MySQL server using the mysql command line client, and queried all the servers.
MySQL Workbench
MySQL Workbench is a GUI tools of MySQL, it is available for Windows, Linux and macOS.
It is easy to create a MySQL connection. To add a connection, click the +
icon to the right of the MySQL Connections title on the home screen. This opens the Setup New Connection form, as the following figure shows.
Type Connection Name, Hostname, Port, Username and Password, and click Test Connection button. If everything is ok, it will prompt you a success tips.
Save the connection, and you can connect the MySQL server by clicking the connection name.
Conclusion
In this article, we introduced several tools for connecting to MySQL server, including mysql command-line client and MySQL Workbench.