Command Line For Mysql In Mac

Posted on

Jun 06, 2019 Start “Terminal”, write these commands and press after each “Enter”: mysql.server start; mysql.server stop; mysql.server restart. In truth, this article is rather a superficial material for beginners who are faced with the problem of running a database on their Mac.

The basic command to work with an existing database is the use command, where you say something like this: mysql use mydatabase For instance, if you have a database named orders, you would declare that you want to start working with it (use it) like this. To start MySQL on Mac, you can use the command line. The commands you use depend on which version of MySQL you have: either before 5.7 or 5.7 and newer. Version 5.7 came out in October 2015, so if you’ve downloaded it anytime in the last few years it’s most likely after 5.7. Dec 08, 2008 The main command line utilities are mysql, mysqldump, and mysqladmin. Many people like the phpMyAdmin package to manage MySQL through a web browser. MySQL is not part of the default install in OS X. Server Administration. I want to run MYSQL from the command line, as in mysql -u root -p but it returns -bash: mysql: command not found So, need to install it, I think. But then what application exactly do I need to. mysql Client Options mysql Client Commands mysql Client Logging mysql Client Server-Side Help Executing SQL Statements from a Text File mysql Client Tips

mysql is a simple SQL shell with input line editing capabilities. It supports interactive and noninteractive use. When used interactively, query results are presented in an ASCII-table format. When used noninteractively (for example, as a filter), the result is presented in tab-separated format. The output format can be changed using command options.

If you have problems due to insufficient memory for large result sets, use the --quick option. This forces mysql to retrieve results from the server a row at a time rather than retrieving the entire result set and buffering it in memory before displaying it. This is done by returning the result set using the mysql_use_result() C API function in the client/server library rather than mysql_store_result().

Alternatively, MySQL Shell offers access to the X DevAPI. For details, see MySQL Shell 8.0 (part of MySQL 8.0).

Using mysql is very easy. Invoke it from the prompt of your command interpreter as follows:



Then type an SQL statement, end it with ;, g, or G and press Enter.

Typing Control+C interrupts the current statement if there is one, or cancels any partial input line otherwise.

You can execute SQL statements in a script file (batch file) like this:

On Unix, the mysql client logs statements executed interactively to a history file. See Section, “mysql Client Logging”.

This should be really straightforward, but I found myself googling around. I wrote a recent post about how to create a web app with the Laravel PHP framework but connecting to the database was tricky.

First step, find out what version of OS X you are running. Click the apple icon in the top right corner of your screen and click “About This Mac”. I got something like this:

Next step, head over to the MySQL downloads page.


So with my computer I am running 10.11 and we want the .dmg file. Download Mac OS X 10.10 (x86, 64-bit), DMG Archive. Then you get to this lame screen:

MySQL is free and open source software (FOSS), you do not need to sign up or create an account. Click “No thanks, just start my download”. I let the download go directly to my “Downloads” folder when prompted, though you could place it anywhere. The download will take some time because it is 350mb, go get a coffee. Once the download is complete. Double click the file and a screen like this will open

Double click it and go through the install process.

When I did it I got an alert box:

Add to path. This went in ~/.zshrc because I am using ZSH. If you are using bash for terminal (the default on OS X) this will be the ~/.bashrc file or ~/.bash_profile. This post explains the difference between the two files.

Add the mysql download to your path so that you can use the “mysql” command from the command line.


Then test it out:

Now that we have MySQL installed and accessible from the Terminal, it’s time to connect to the database and start the MySQL server. Let’s log in as root user ([email protected]) and use that funky password that popped up in the alert window after our install. In the above case it’s #fW&tYe?<8?w

Great! Now we can create databases and run SQL queries right from the command line. That’s an awesome start, but there’s no way anyone is going to remember that password, nor is it enjoyable to type in every time we want to run MySQL. The full docs on changing passwords are here. For OSX you can run the command:

The PASSWORD('root') sets my password to “root”. Easy to remember right? You can set your password to anything you like, such as PASSWORD('[email protected]').

To stop the server in the terminal run control + D. Congratulations! You’ve installed MySQL on OSX, made it accessible from the command line and updated your password for the root user.

To start setting up a Laravel application and connect to a database using Sequel Pro you can follow my next tutorial:

Thanks for reading!


Update (9/16/17): Expired passwords

When trying to login to MySQL on OSX recently I ran into the following error:

There are a lot of StackOverflow threads and articles about this, many require writing SQL commands, though that was difficult for me without access to the terminal. In order to alleiviate this I found this SO answer that saved me:

Command line for mysql in mac os

Update 03/29/18:

  • Helpful steps for Install MySQL on macOS Sierra as a gist link.

Update 05/14/19:

  • I recently ran into an error when trying to fire up the MySQL shell:

Command Line For Mysql In Mac Version

I found the solution in this homebrew issue. Create an empty directory for this path: mkdir /usr/local/etc/my.cnf.d