从命令行连接到 MySQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5131931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
connecting to MySQL from the command line
提问by Leahcim
How can you connect to MySQL from the command line in a Mac? (i.e. show me the code)
如何从 Mac 的命令行连接到 MySQL?(即给我看代码)
I'm doing a PHP/SQL tutorial, but it starts by assuming you're already in MySQL.
我正在做一个 PHP/SQL 教程,但它首先假设您已经在 MySQL 中。
回答by Nishant
See here http://dev.mysql.com/doc/refman/5.0/en/connecting.html
见这里http://dev.mysql.com/doc/refman/5.0/en/connecting.html
mysql -u USERNAME -pPASSWORD -h HOSTNAMEORIP DATABASENAME
The options above means:
上面的选项意味着:
-u: username
-p: password (**no space between -p and the password text**)
-h: host
last one is name of the database that you wanted to connect.
Look into the the link, it's detailed there!
查看链接,那里有详细说明!
As already mentioned by Rick, you can avoid passing the password as the part of the command by not passing the password like this:
正如Rick已经提到的,您可以通过不像这样传递密码来避免将密码作为命令的一部分传递:
mysql -u USERNAME -h HOSTNAMEORIP DATABASENAME -p
People editing this answer: PLEASE DONOT ADD A SPACE between -p
and PASSWORD
编辑此答案的人:请不要在-p
和之间添加空格PASSWORD
回答by Rick
Best practice would be to mysql -u root -p
.
Then MySQL will prompt for password after you hit enter.
最佳实践是mysql -u root -p
. 然后 MySQL 会在您按 Enter 键后提示输入密码。
回答by Swamy
Use the following command to get connected to your MySQL database
使用以下命令连接到您的 MySQL 数据库
mysql -u USERNAME -h HOSTNAME -p
mysql -u 用户名 -h 主机名 -p
回答by Name Surname
After you run MySQL Shell and you have seen following:
运行 MySQL Shell 后,您会看到以下内容:
mysql-js>
Firstly, you should:
首先,你应该:
mysql-js>\sql
Secondly:
其次:
mysql-sql>\connect username@servername (root@localhost)
And finally:
最后:
Enter password:*********
回答by arsho
One way to connect to MySQL directly using proper MySQL username and password is:
使用正确的 MySQL 用户名和密码直接连接到 MySQL 的一种方法是:
mysql --user=root --password=mypass
Here,
这里,
root is the MySQL username
mypass is the MySQL user password
This is useful if you have a blank password.
如果您的密码为空,这将很有用。
For example, if you have MySQL user called root
with an empty password, just use
例如,如果您root
使用空密码调用 MySQL 用户,只需使用
mysql --user=root --password=
回答by Anand Tiwari
This worked for me ::-
这对我有用 ::-
mysql --host=hostNameorIp --user=username --password=password
or
或者
mysql --host=hostNameorIp --user=username --password=password database_name