`sudo mysql`(不带标志)做什么?

huangapple go评论44阅读模式
英文:

What does `sudo mysql` (without flag) do?

问题

我是新手使用mysql。我以为它会连接到root,但我不确定是否这样,因为当我运行create database hello;时,它显示:

无连接。尝试重新连接...
错误 1049 (42000): 未知数据库 'hello'
错误:
无法连接到服务器

(我期望它只是创建数据库)

我在Ubuntu 23上,本地运行mysql。

英文:

I am new to mysql. I assumed it would connect me as root but I am not sure it does that because when I then run create database hello; it says:


No connection. Trying to reconnect...
ERROR 1049 (42000): Unknown database 'hello'
ERROR:
Can't connect to the server

(I expected it to just create the database)

I am on ubuntu 23, running mysql locally.

答案1

得分: 1

sudo mysql将会以root身份登录到您的本地mysql服务器。我看到的错误与另一个地方有关。

您尝试创建数据库。Mysql失败,并显示以下错误:

没有连接。尝试重新连接...
然后尝试切换到创建的数据库:

错误1049(42000):未知数据库 'hello'
错误:
无法连接到服务器

因此,您必须查明为什么会出现关于连接断开的错误。您得到的错误与您的问题标题无关。
我期望即使您以具有权限的用户身份登录,也不会发生任何变化。

英文:

sudo mysql will login you as root in your local mysql server. the error I am seeing is related to a different point

You try to create the database. Mysql fails with this error:

>No connection. Trying to reconnect...

then tries to switch to the created database:

>ERROR 1049 (42000): Unknown database 'hello'
ERROR:
Can't connect to the server

so you have to dig why you get this error about connection dropping. The error you get has nothing to do with the title of your question.
I expect nothing will change even if you log in as a user with rights

答案2

得分: 1

使用只有

sudo mysql

而不指定数据库会连接到使用配置中的默认设置的数据库。您可以通过检查以下命令的输出来查看这些设置:

mysql --help

或者

mysql --help | grep -E 'database|port|user|host'

以获取您可能关心的变量。

在第一个命令的输出中,您还将看到配置文件的位置。类似于:

/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

要连接到不同的服务器/数据库或使用不同的凭据,您可以修改这些配置文件,或者简单地运行

mysql --user={用户名} --password={密码} --database={数据库名称} --host={主机名}

用您想要的值替换这些值。

英文:

Using just

sudo mysql

without specifying a database would connect to a db using the default settings in your configuration. You can see these by checking the output of

mysql --help

or

mysql --help | grep -E 'database|port|user|host'

to get the variables you most likely care about.

In the output of the first command you will also see where this config file is. Something like:

/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf 

To connect to a different server/database or using different credentials you can modify these config files, or simply run

mysql --user={username} --password={password} --database={database_name}  --host={host_name} 

replacing the values with the ones you want.

huangapple
  • 本文由 发表于 2023年7月13日 22:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定