Question

I am following this tutorial: http://dev.mysql.com/doc/refman/5.7/en/connecting-disconnecting.html

However when I run MySql command line client I automatically get asked for a password when I enter it I get connected automatically to a localhost database, how do I access other database's using something like the following as shown in the tutorial example?

shell> mysql -h host -u user -p
Enter password: ********
Was it helpful?

Solution

shell> mysql nameofdatabase -uusername -ppassword

You can put a space in between -u username but not between the -p and password.

OTHER TIPS

There are multiple ways to access mysql on the command line. If you wish to connect to another database server, you need to add -h <hostname> to your mysql command. Without this, mysql assumes you want to connect to the local mysql server.

If you mean that you want to connect to another database on a host, just specify that database at the end of your command line.

$ mysql -u username -ppassword second_database

Note, there is NO space between -p and password. If -p is followed by a space, mysql will prompt you to enter a password interactively. Which you can do, if that is what you want.

$ mysql -u username -p third_database
Enter password: *********

Another way to connect would be to create a file in your home directory named .my.cnf. This file should contain the following:

[client]
user=username
password=yourpassword

If you have any special characters in your password, you will need to quote it. Having this file allows you to not need to enter any username or password on your command line:

$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
....
mysql>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top