mysql Command Line
Note
This material is optional.
The mysql
command-line utility comes with MySQL Workbench.
You can use this utility to automate various tasks, such as creating a database.
Step 1: Running mysql
¶
Open a terminal
and run the command mysql --version
.
If the command runs successfully, you should see the installed version number.
If you get an error saying the command is not found, then you need to modify your system PATH
.
-
On Linux, you might need to install the package:
sudo apt install mysql-client-core-8.0
-
On macOS, add the following line to your
~/.zshrc
file (or~/.bash_profile
if you have an older Mac from before 2021). Then quit and restart the Terminal app.export PATH=${PATH}:/Applications/MySQLWorkbench.app/Contents/MacOS
-
On Windows, search for
env
in the start menu and click "Edit the system environment variables." Then click the Environment Variables button, click "Path", and click "Edit…" Add the following path to the list:C:\Program Files\MySQL\MySQL Workbench 8.0 CE\
Note
You can also run mysql
on stu.cs.jmu.edu, if the command doesn't work on your laptop.
Step 2: Connecting to data.cs.jmu.edu¶
By default, mysql
attempts to connect to localhost
.
Use the following command-line arguments to connect to our server.
mysql -h data.cs.jmu.edu -u username -p
Alternatively, you can store these arguments in an option file.
For example, you can add the following options to ~/my.cnf
(on Linux and macOS):
[client]
host=data.cs.jmu.edu
port=3306
user=...
password=...
After connecting:
- Type
USE username
(replace with your username) to select your database. - Type
SOURCE filename.sql
(replace with your filename) to run a script.
Step 3: Official MySQL tutorial¶
Chapter 5 Tutorial of the MySQL documentation provides an excellent overview of this week's topics.
In the remaining time, work through the tutorial, using the mysql
client to run the examples.