To connect to a MySQL server, you can use the mysql command-line client. Here’s how to do it:
-
Open your terminal.
-
Use the following command to connect:
mysql -u username -pReplace
usernamewith your MySQL username (e.g.,root). -
Enter your password when prompted.
After you run the command, you will be prompted to enter the password for the specified user. Type your password and press Enter.
-
If the connection is successful, you will see the MySQL prompt:
mysql>
From here, you can execute SQL commands and queries.
Example:
To connect as the root user, you would run:
mysql -u root -p
Then enter the password when prompted. If you want to connect to a specific database right away, you can add the database name at the end:
mysql -u username -p database_name
Note:
-
Ensure that the MySQL server is running before attempting to connect.
-
If you are connecting to a remote MySQL server, you can specify the host with the
-hoption:mysql -h hostname -u username -p
Replace hostname with the IP address or domain name of the MySQL server.
