尝试在新安装的 Ubuntu WSL 上初始化不安全的 MySQL。

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

Trying to initialize insecure mysql after a fresh install on ubuntu WSL

问题

I want to initialize insecure mysql, but even with a fresh install It does not work:

我想初始化不安全的 MySQL,但即使是全新安装也无法运行:

mysqld --initialize-insecure
mysqld: 无法创建目录 '/var/lib/mysql/'(操作系统错误号 17 - 文件已存在)
2023-05-22T17:35:25.059166Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld(mysqld 8.0.33-0ubuntu0.20.04.2)正在进行服务器初始化,进程编号为 8886
2023-05-22T17:35:25.059914Z 0 [ERROR] [MY-010187] [Server] 无法打开文件 '/var/log/mysql/error.log' 以记录错误:权限被拒绝
2023-05-22T17:35:25.059949Z 0 [ERROR] [MY-013236] [Server] 指定的数据目录 /var/lib/mysql/ 不可用。您可以删除服务器添加到其中的所有文件。
2023-05-22T17:35:25.059954Z 0 [ERROR] [MY-010119] [Server] 中止
2023-05-22T17:35:25.060000Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld:关闭完成(mysqld 8.0.33-0ubuntu0.20.04.2)(Ubuntu)。

我尝试将日志设置为 mysql:mysql,并使用空的 /var/lib/mysql/ 但似乎不起作用
我应该怎么做才能进行初始化?

EDIT:

感谢 Zak,我现在可以运行该命令(我将文件夹权限更改为我的用户,而不是 mysql)

```plaintext
[Warning] [MY-010453] [Server] root@localhost 使用空密码创建!请考虑关闭 --initialize-insecure 选项。

然而:

```plaintext
mysql -u root
ERROR 1698 (28000): 用户 'root'@'localhost' 访问被拒绝。

根据我了解的情况,应该可以使用 root。
PS:这是为了开发站点,不用于生产。
英文:

I want to initialize insecure mysql, but even with a fresh install It does not work :

mysqld --initialize-insecure
mysqld: Can't create directory '/var/lib/mysql/' (OS errno 17 - File exists)
2023-05-22T17:35:25.059166Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.33-0ubuntu0.20.04.2) initializing of server in progress as process 8886
2023-05-22T17:35:25.059914Z 0 [ERROR] [MY-010187] [Server] Could not open file '/var/log/mysql/error.log' for error logging: Permission denied
2023-05-22T17:35:25.059949Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
2023-05-22T17:35:25.059954Z 0 [ERROR] [MY-010119] [Server] Aborting
2023-05-22T17:35:25.060000Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.33-0ubuntu0.20.04.2)  (Ubuntu).

I tried setting the log to mysql:mysql with an empty /var/lib/mysql/ but it does not seems to work
What should I do to make an initialize insecure ?

EDIT :

Thanks to Zak, I can now run the command (I chmod the folder permission to my user instead of mysql)

[Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

However :

mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

From what I understand root should work here.

PS: This is for a dev station, it is not for production

答案1

得分: 1

以下是翻译好的部分:

现在您在这里并可以尝试登录... 但root是空的,所以它将始终失败... 您需要执行以下步骤来“保护”您的设置。

我只回答这个问题,因为这是开发机器... 我绝不建议在存有敏感数据的生产机器上跳过授权。

注意: 我在此示例中使用了vim... 但您可以使用NANO或您感觉舒适的任何编辑器。 关键是将skip-grant-tables添加到my.cnf文件的末尾——这几乎使MySQL不需要登录即可正常工作,从而允许我们更改root的密码。 然后,在更改密码后,我们从my.cnf中删除skip-grant-tables并重新启动mysql以要求有效的凭据。

要查找配置文件的位置,您可以运行mysqladmin --help,这应该会给您类似以下内容:

$ mysqladmin --help
...
默认选项按给定顺序从以下文件中读取:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
...

如果打开文件并看到类似以下内容:

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

这些是您实际配置文件的位置。 它只是包含这些目录中的所有文件... 只需选择最后一个文件并将skip授权放在该文件的末尾。

现在您可以开始操作。

$ sudo su
$ vi /etc/my.cnf

在文件末尾添加skip-grant-tables。 保存并退出。

$ service mysql restart
$ service mysql status
mysql.service - MySQL Community Server
已加载:已加载(/lib/systemd/system/mysql.service;启用;供应商预设:已启用)
活动:自2022年08月12日18:45:46 CDT以来运行;9个月8天前
主PID:970(mysqld)
状态:“服务器正常运行”
任务:45(限制:19110)
内存:878.3M
CGroup:/system.slice/mysql.service
     └─970 /usr/sbin/mysqld
$ mysql
登录消息
mysql> use mysql;
mysql> UPDATE user SET `authentication_string` = PASSWORD('ABC123') WHERE `User` = 'root'; 
mysql> exit
Bye
$ vi /etc/my.cnf

删除行—>skip-grant-tables 保存并退出。

$ service mysqld restart
$ service mysql status
mysql.service - MySQL Community Server
已加载:已加载(/lib/systemd/system/mysql.service;启用;供应商预设:已启用)
活动:自2022年08月12日18:45:46 CDT以来运行;9个月8天前
主PID:970(mysqld)
状态:“服务器正常运行”
任务:45(限制:19110)
内存:878.3M
CGroup:/system.slice/mysql.service
     └─970 /usr/sbin/mysqld
$ exit
$ mysql -u root -p
英文:

Now that you are here and able to TRY to log in .. But root is empty so it will always fail .. You need to do the following steps to "secure" your setup.

I am only answering this OP, because it's a dev machine .. I would never recommend skipping grant on a production machine with sensitive data stored.

NOTE: I use vim in this example .. But you can use NANO or whatever editor you feel comfortable using. The point is to add skip-grant-tables to the end of the my.cnf file -- Which pretty much makes MySQL not need a login at all to function, thus allowing us to change the password for root. Then we remove the skip-grant-tables from my.cnf after we've changed the password and restart mysql to require valid credentials.

To find the location of your config file(s) you may run a mysqladmin --help -- This should give you something like:

$ mysqladmin --help
...
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
...

If you open the file and see something like:

 !includedir /etc/mysql/conf.d/
 !includedir /etc/mysql/mysql.conf.d/

Those are the locations of your actual configuration fiile. It is just including all the files in those directories.. Just choose the last file and put the skip grants at the end of that file.

You can now get started.

$ sudo su
$ vi /etc/my.cnf

Add skip-grant-tables To the end of the file. Save and exit.

$ service mysql restart
$ service mysql status
mysql.service - MySQL Community Server
    Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2022-08-12 18:45:46 CDT; 9 months 8 days ago
    Main PID: 970 (mysqld)
    Status: "Server is operational"
    Tasks: 45 (limit: 19110)
    Memory: 878.3M
    CGroup: /system.slice/mysql.service
         └─970 /usr/sbin/mysqld

$ mysql
Login Message
mysql> use mysql;
mysql> UPDATE user SET `authentication_string` = PASSWORD('ABC123') WHERE `User` = 'root'; 
mysql> exit
Bye
$ vi /etc/my.cnf

Erase line --> skip-grant-tables Save and exit.

$ service mysqld restart
$ service mysql status
mysql.service - MySQL Community Server
    Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2022-08-12 18:45:46 CDT; 9 months 8 days ago
    Main PID: 970 (mysqld)
    Status: "Server is operational"
    Tasks: 45 (limit: 19110)
    Memory: 878.3M
    CGroup: /system.slice/mysql.service
         └─970 /usr/sbin/mysqld
$ exit
$ mysql -u root -p

huangapple
  • 本文由 发表于 2023年5月23日 01:45:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308729.html
匿名

发表评论

匿名网友

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

确定