3

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: ********
1
  • Do you mean other databases on the same host, or other database hosts? Commented Feb 4, 2014 at 2:51

2 Answers 2

3
shell> mysql nameofdatabase -uusername -ppassword

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

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

3 Comments

Thanks for the reply. I do not have shell> on my command line, as soon as I type in my password (which is initially asked for when I load the terminal) I get it starting with mysql>
Actually, this is not correct. If you leave a space between -p and password it will prompt you to enter the password interactively and use "password" as the database that you wish to use.
I'm editing my answer as the ability to leave a space after -p is NOT possible. I apologize for the error.
1

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>

Comments

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.