MySQL login FAQ: How do I log in to a MySQL database?

MySQL login FAQ: How do I log into a MySQL database?

Solution

Assuming that you (a) have the root user password and (b) you are logging into a MySQL database on your local computer, this mysql command from your Unix/Linux command line is what you need:

mysql -u root -p

After issuing that MySQL login command, you’ll be prompted for the root user password. Just enter that root password, and you should be in. After you login, your console should look like this (using MySQL version 5):

C:\>mysql -u root -p
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 5.0.18-nt

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

If you are not the MySQL root user ...

If you’re not the MySQL root user, the command you use is still basically the same:

mysql -u my_username -p

A big difference with this command is that you will probably be limited to the certain databases that you are allowed to access. You should be able to log in like that, and then supply your password after the mysql command prompts you for that.

If that doesn’t work, you may also need to supply the database name, like this:

mysql -u my_username my_database_name -p

If your database is on a different host ...

If your MySQL database is on a different host, you’ll need to use the -h option:

mysql -u my_username -h my_server.com my_database_name -p

In all of these examples, note that you can use the longer command-line options, which look like this:

mysql --user=my_username --host=my_server.com my_database_name --password=

I need to say that I haven’t tested that --password option, so I’m not 100% sure if the trailing = symbol is needed or not.

MySQL SSL mode

As a final note, if you want to use SSL (encryption) when logging in, you can also use the --ssl-mode option. It doesn’t take any parameters, you just add it to the previous commands like that:

mysql --ssl-mode --user=my_username --host=my_server.com my_database_name --password=

In summary, if you needed to see different ways you can log into a MySQL or MariaDB server from a Unix or Linux command line with the mysql command, I hope these examples are helpful.