我写这个答案是因为我认为我们都误解了 mariadb 的User Account Management 机制。我也遇到了same problem as you,所以我在为自己写这个答案。您可以按照您实际需要的答案或danblack 进行操作。正如danblack所说,将“mysql”更改为您的service_account可能不是一个好主意。
使用 'mysql' 用户帐户初始化 mariadb 数据库目录:
$ 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 帐户。
然后,启用并启动 mariadb 服务器:
$ sudo systemctl enable --now mariadb.service
您可能想使用mysql_secure_installation 命令来improve the initial security。
然后使用 mariadb root@localhost 帐户和系统 root 帐户连接到服务器,并使用您想要的任何名称 create new mariadb account that has root-like privileges:
$ 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 帐户连接到服务器并创建一个新数据库:
$ 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 的情况下使用此帐户:
$ 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