Home >Database >Mysql Tutorial >How Do I Connect to a MySQL Database from the Command Line on a Mac?
Connecting to MySQL from the Command Line on a Mac
When working on a Mac, establishing a connection to MySQL via the command line is often a crucial step. This can be easily achieved using the mysql command, providing essential access to the MySQL database system.
To connect to MySQL from the command line, execute the following command:
mysql -u USERNAME -pPASSWORD -h HOSTNAMEORIP DATABASENAME
Here, "-u" represents the username, "-p" followed immediately by the password without any space, "-h" denotes the hostname or IP, and the last parameter represents the target database.
For example, if the username is "my_username", the password is "my_password", the hostname is "localhost", and the database name is "my_database", the command would be:
mysql -umy_username -pmy_password -hlocalhost my_database
After running this command, you will be prompted to enter the password. Once provided, you should be successfully connected to the MySQL database.
Another option to connect to MySQL is to omit the password in the command, then provide it interactively after pressing Enter. This can be done using the following command:
mysql -u USERNAME -h HOSTNAMEORIP DATABASENAME -p
For security reasons, however, it is generally recommended to include the password directly in the command to avoid potential eavesdropping.
The above is the detailed content of How Do I Connect to a MySQL Database from the Command Line on a Mac?. For more information, please follow other related articles on the PHP Chinese website!