2

I'm using MariaDB 10.5.9-1 on Manjaro 5.10.23-1.

I initialized mariadb database directory using this command:

$ sudo mariadb-install-db --user=shaouai --basedir=/usr --datadir=/var/lib/mysql

After data directory initialization, permissions and ownership of /var/lib/mysql:

$ ls -ld /var/lib/mysql
drwx------ 5 shaouai root 4096 Apr 10 00:08 mysql
$ ls -lh /var/lib/mysql
total 109M
-rw-rw---- 1 shaouai shaouai  24K Apr 10 00:08 aria_log.00000001
-rw-rw---- 1 shaouai shaouai   52 Apr 10 00:08 aria_log_control
-rw-rw---- 1 shaouai shaouai  972 Apr 10 00:08 ib_buffer_pool
-rw-rw---- 1 shaouai shaouai  12M Apr 10 00:08 ibdata1
-rw-rw---- 1 shaouai shaouai  96M Apr 10 00:08 ib_logfile0
drwx------ 2 shaouai shaouai 4.0K Apr 10 00:08 mysql
drwx------ 2 shaouai shaouai 4.0K Apr 10 00:08 performance_schema
drwx------ 2 shaouai shaouai 4.0K Apr 10 00:08 test

And there's one another directory /run/mysqld, which file mysqld.sock resides in by default when server is up and running, has permissions and ownership:

$ ls -ld /run/mysqld/
drwxr-xr-x 2 shaouai shaouai 60 Apr 10 08:31 /run/mysqld/

I have no idea when /run/mysqld was created, maybe the first time server started successfully.

mysqld_safe --datadir=/var/lib/mysql can start server successfully.

But the problem is, after every reboot, the permissions and ownership of /var/lib/mysql and /run/mysqld was reset to mysql:mysql :

$ ls -ld /var/lib/mysql
drwx------ 5 mysql mysql 4096 Apr 10 00:08 mysql
$ ls -lh /var/lib/mysql
total 109M
-rw-rw---- 1 shaouai shaouai  24K Apr 10 00:08 aria_log.00000001
-rw-rw---- 1 shaouai shaouai   52 Apr 10 00:08 aria_log_control
-rw-rw---- 1 shaouai shaouai  972 Apr 10 00:08 ib_buffer_pool
-rw-rw---- 1 shaouai shaouai  12M Apr 10 00:08 ibdata1
-rw-rw---- 1 shaouai shaouai  96M Apr 10 00:08 ib_logfile0
drwx------ 2 shaouai shaouai 4.0K Apr 10 00:08 mysql
drwx------ 2 shaouai shaouai 4.0K Apr 10 00:08 performance_schema
drwx------ 2 shaouai shaouai 4.0K Apr 10 00:08 test
$ ls -ld /run/mysqld/
drwxr-xr-x 2 mysql mysql 60 Apr 10 08:31 /run/mysqld/

Thus due to various "permission denied", MariaDB server failed to start.

There's one question totally the same as mine.

Here is full output of systemctl cat mariadb.service.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
shaouai
  • 55
  • 5

1 Answers1

2

I'm not sure why you changed the user. It may not be a good idea. But assuming there is a good reason, below explains your situtation:

It looks like the mysqld_safe may have changed the permissions. If you start this way for testing add --user=shaouai to the arguments.

To allow systemd to default start as the user shaouai

systemctl edit mariadb.service

Add:

[Service]
User=shaouai
Group=shaouai

Then:

chown -R shaouai: /var/lib/mysql /run/mysqld/

Then systemctl restart mariadb.service.

This will start the service as this user instead of the default mysql user.

danblack
  • 12,130
  • 2
  • 22
  • 41
  • I changed "mysql" to my user name because I thought I have to login as the system "mysql" user to connect the server, just as "The second is mysql@localhost, it has no password either, but you need to be the system 'mysql' user to connect" indicated. Am I totally wrong ? – shaouai Apr 10 '21 at 02:41
  • If I can connect to the sever and don't have to be the system "mysql" user, I'll just initialize datadir with "mysql". Now I think it's totally possible according to your words. I'm so grateful. – shaouai Apr 10 '21 at 03:17
  • Yes, a mysql@localhost with [unix socket auth](https://mariadb.com/kb/en/authentication-plugin-unix-socket/#security). Use `show create user` to display the current user and `show create user root@localhost` to show another user. `select user,host from mysql.user` shows the current users. You can create your own users with [create user](https://mariadb.com/kb/en/create-user/). All of these are independent to what user the database is running as. – danblack Apr 10 '21 at 07:31
  • 1
    A lot of thanks @danblack. I've written my answer in which I initialized datadir with "mysql", the link is [here](https://stackoverflow.com/a/67032184/15003321) if you like. – shaouai Apr 10 '21 at 08:37