1

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

3
  • Yes, there is the command line client, conveniently called mysql. Commented Nov 13, 2015 at 17:10
  • yes, mysql: command not found, how to install this? Commented Nov 13, 2015 at 17:11
  • Probably a dupe of stackoverflow.com/questions/8195418/… or several others Commented Nov 13, 2015 at 17:12

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

I've just updated the post (as you posted that comment) to address that issue :)
How do I know that mysql is installed on my macbook?
Well that's an entirely different question, and quite a long way from the scope of this one.... have you actually installed it? You can hardly expect it to work if it isn't there.
I never installed such thing. Is there a some tutorial how to install this?
Yes, many.... Google is probably your friend here. Do you want to access MySQL running on the MacBook, though, or do you want to connect to a DIFFERENT MySQL server from the Mac's command line?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.