从命令行连接到 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:55:06  来源:igfitidea点击:

connecting to MySQL from the command line

mysql

提问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 -pand 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 rootwith 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