How to connect to MySQL server from the console Pavel Fedoruk, 11 May 2024 at 06:07 PM

The official documentation on this matter is available here.

mysql -u USERNAME -pPASSWORD -h HOSTNAMEORIP DATABASENAME

The arguments are as follows:

-u: username
-p: password (**no space between -p and the password text**)
-h: host IP or domain name
-DATABASENAME: name of database (not required)

host (-h) is only used when connecting to a specific IP address of the host.

Example:

mysql -u root -p123456789abcd -h 127.0.0.1 database123

It should be noted that specifying the password directly in the terminal console is not secure (the mysql shell will also warn about this), so the command should be specified like this:

mysql -u USERNAME -h HOSTNAMEORIP DATABASENAME -p

In this case, after executing this command, mysql will prompt you to enter the password separately, thus it will not be saved in the terminal history.

Example:

mysql -u root -h 127.0.0.1 database123 -p

Other recipes

In case you haven't read them yet