问题描述
我正在使用适用于 Linux (x86_64) 的 MySQL 发行版 10.2.39-MariaDB.当我们最初启动它并手动配置它以正确所有权和启动时,它运行良好.但是每当我们重新启动 Linux 服务器时,'/var/lib/mysql' 的所有权就会从 'service_account' 变为 'mysql',如下所示:
I'm using MySQL distribution 10.2.39-MariaDB, for Linux (x86_64). It runs fine when we initially start it up and manually configure it to correct ownership and startup. But whenever we reboot the Linux server, the ownership of '/var/lib/mysql' changes from 'service_account' to 'mysql', as shown below:
Linux 服务器重启前
drwxr-xr-x. service_account service-account_grp 4096 Mar 18 14:00 mysql
到
Linux 服务器重启后
drwxr-xr-x. mysql mysql 4096 Mar 18 14:00 mysql
它本身改变了所有权,我碰巧没有找到它的根本原因.我一直面临这个问题,但我无法找到解决方案.
It changes ownership itself and I don't happen to find the root cause for it.I've been facing this issue and I'm not able to find a solution to it.
感谢任何输入.
提前致谢,
席德
推荐答案
我写这个答案是因为我认为我们都误解了 用户账户管理 mariadb的机制.我也遇到了和你一样的问题,所以我在为自己写这个答案.您可以按照此答案或 danblack 的答案进行操作,无论您实际需要什么.正如danblack所说,改变mysql"可能不是一个好主意.到您的 service_account.
I'm writing this answer because I think we both have misunderstood the User Account Management mechanisms of mariadb. I have also encountered the same problem as you, so I'm writing this answer for myself somewhat. You can follow this answer or danblack's, whichever you actually need. As danblack said, it may not be a good idea to change "mysql" to your service_account.
使用 'mysql' 用户帐户初始化 mariadb 数据库目录:
Initialize mariadb database directory with 'mysql' user account :
$ sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
...
Two all-privilege accounts were created.
One is root@localhost, it has no password, but you need to
be system 'root' user to connect. Use, for example, sudo mysql
The second is mysql@localhost, it has no password either, but
you need to be the system 'mysql' user to connect.
After connecting you can set the password, if you would need to be
able to connect as any of these users with a password and without sudo
...
如您所见,创建了两个 mariadb 全权限帐户,root@localhost
和 mysql@localhost
.而且你必须是操作系统root
和mysql
用户才能分别使用这两个mariadb账号.
As you can see, two mariadb all-privilege accounts, root@localhost
and mysql@localhost
, were created. And you must be operating system root
and mysql
user to use these two mariadb accounts, respectively.
然后,启用并启动 mariadb 服务器:
After then, enable and start mariadb server :
$ sudo systemctl enable --now mariadb.service
您可能想使用 mysql_secure_installation
命令来改进初始安全.
You may want to use mysql_secure_installation
command to improve the initial security.
然后使用mariadb root@localhost
帐号和系统root
帐号连接服务器,创建具有类似 root 权限的新 mariadb 帐户 使用您想要的任何名称:
Then use mariadb root@localhost
account with system root
account to connect to sever, and create new mariadb account that has root-like privileges with whatever name you want:
$ sudo mysql -u root -p # first input your passwd to use 'sudo', then [ENTER] for empty mariadb root account passwd
[sudo] password for your_service_account:
Enter password:
...
MariaDB [(none)]> CREATE USER 'whatever_name_you_want'@'localhost' IDENTIFIED BY 'somepasswd';
Query OK, 0 rows affected (0.004 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* to 'whatever_name_you_want'@'localhost';
Query OK, 0 rows affected (0.004 sec)
MariaDB [(none)]> quit
Bye
使用新的非特权 whatever_name_you_want@localhost
帐户连接到服务器并创建一个新数据库:
Use new unprivileged whatever_name_you_want@localhost
account to connect to server and create a new database :
$ mysql -u whatever_name_you_want -p
Enter password:
...
MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS mydb;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> quit
Bye
如果你为 root@localhost
账户设置密码,你甚至可以在没有 sudo 的情况下使用这个账户:
And if you set password for root@localhost
account, you can even use this account without sudo :
$ sudo mysql -u root -p
[sudo] password for your_service_account:
Enter password:
...
MariaDB [(none)]> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('somepasswd');
Query OK, 0 rows affected (0.041 sec)
MariaDB [(none)]> quit
Bye
$ mysql -u root -p
Enter password:
...
MariaDB [(none)]> quit
Bye
这篇关于MySQL:在 Linux 服务器重新启动时,mysql 目录的所有权从“service_account"更改为“mysql"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!