Establishing a MySQL Command Line Connection
Connecting to MySQL from the command line on a Mac is essential for managing databases and performing data operations. This article will provide you with a step-by-step guide to connect to MySQL, showcasing the necessary code.
Procedure
-
Open Terminal: Launch the Terminal application from the Utilities folder of your macOS or access it through the Spotlight search.
-
Install MySQL Client: If you haven't already, install the MySQL client using the Homebrew package manager:
brew install mysql-client
-
Connect to MySQL: Establish a connection to the MySQL server using the mysql command along with your login credentials:
mysql -u <username> -p<password> -h <hostname or IP> <database name>
- Replace with your MySQL username.
- Replace with your MySQL password (avoid adding a space after -p).
- Replace with the hostname or IP address of the MySQL server.
- Replace with the name of the database you want to connect to.
Example:
mysql -u myuser -pmypassword -h localhost mydatabase
-
Enter Password: If you didn't pass the password in the command, you will be prompted to enter it at the terminal.
-
Verify Connection: Upon successful login, you will see the MySQL prompt:
mysql>
Additional Notes
- You can avoid entering the password in the command by specifying the -p option followed by the password without a space: mysql -u -p...
- For more detailed instructions and options, refer to the MySQL documentation provided in the answer: [http://dev.mysql.com/doc/refman/5.0/en/connecting.html](http://dev.mysql.com/doc/refman/5.0/en/connecting.html)
The above is the detailed content of How to Connect to MySQL from the Command Line on a Mac?. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn