Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61840

How to connect to MySQL database on server via Terminal on Macbook?

Is there a way to connect to my MySQL database and do something on tables via terminal?

Upvotes: 1

Views: 6108

Answers (1)

Jon Story
Jon Story

Reputation: 3051

Yes. In your terminal start the mysql prompt using

mysql --user=user_name --password=your_password db_name

Where db_name is the name of your database and user_name and password are your username and password.

You can then run SQL statements/queries from .sql files

mysql db_name < script.sql > output.tab

Where db_name is your database name, script.sql is a file containing your script, and output.tab (optional) is a file in which to dump the output of the query

You then simply place an SQL query in a file and run it.


If you get the error mysql: command not found, this is because the mysql executable cannot be found in your system PATH. If so, you need to run the following command to add the mySQL folder to the PATH, so that OS X knows to look there for the executable

export PATH=${PATH}:/usr/local/mysql/bin

Where /usr/local/mysql is the location of your mysql installation.

You can add this to your .bash_profile file (located at ~\.bash_profile, or you can create it) in order to have it run every time you start a new terminal. Otherwise you'll have to enter it manually before using the mysql command

Once you've entered this command (or added it to .bash_profile) you can use the mysql command as above

Alternately navigate to /usr/local/mysql/bin (or the location of your mysql install) and use the command

./mysql command

Instead of

mysql command

As above (where command is the command described in the first half of this post). This runs the mysql binary directly, rather than searching for it in the PATH

Upvotes: 3

Related Questions