Jason Young
Jason Young

Reputation: 439

How do I run mysqld as root in the official MySql docker image?

First off, I know not to run as root normally. I have an abnormal situation: I need to use mysqldump with the --tab argument, which requires permission to write to disk, and I want to use those files outside the Docker container. I could explain why running mysqld as root makes this easier, but isn't this question long enough? Running as root is safe in this case because the container will be used only for running tests and for updating DB backup scripts based on SQL migration scripts, and it will be started to do 1 job and then taken back down again.

When I google for how to run mysqld as root, I find the answer indirectly given in instructions on how to NOT run as root. Among other things in order to run mysqld as user_name:

Start the server as user user_name. Another alternative is to start mysqld as the Unix root user and use the --user=user_name option.

To start the server as the given user automatically at system startup time, specify the user name by adding a user option to the [mysqld] group of the /etc/my.cnf option file or the my.cnf option file in the server's data directory.

Do we do one of those? Both of those? I'll assume both just in case. But do they really mean /etc/my.cnf, or does that depend on the installation (e.g. what Linux distribution)? E.g. Docker image mysql:5.6 has /etc/mysql/my.cnf. The directions for the MySql Docker image advise mounting a volume at /etc/mysql/conf.d which is referenced in the aforementioned my.cnf. (Doing so overwrites 2 configuration files that are there by default, so I used a COPY command in my Dockerfile instead to merely add a config file.) The file does make it into the container:

root@4f612d10a690:/etc/mysql/conf.d# cat my.cnf
[mysqld]
user=root

One further requirement from the MySql manual is to add the --user=root argument to mysqld. The official MySql image calls mysqld via its CMD, so I override that in my Dockerfile. My CMD command does indeed run (it is run in 2 places in official MySql image's entrypoint script):

# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
mysql        1  0.1  2.8 1452788 472756 ?      Ssl  14:24   0:01 mysqld --user=root

Note that mysqld has the --user=root command I provided, but is running as the mysql user, not as root.

Here's my full Dockerfile:

FROM mysql:5.6
VOLUME ["/var/lib/mysql-files"]
COPY ["my.cnf", "/etc/mysql/conf.d"]
CMD ["mysqld", "--user=root"]

My only guess as to why it's not running as root is that they mysql image's entrypoint script changes to the mysql user before running:

# allow the container to be started with `--user`
if [ ...blah... -a "$(id -u)" = '0' ]; then
    ...blah...
    exec gosu mysql "$BASH_SOURCE" "$@"
fi

The above snippet basically says, if the user is root, then run the supplied arguments (the CMD + args in this case) as the mysql user.

Is running mysqld as root simply not supported by the official MySql Docker image?

Upvotes: 3

Views: 10090

Answers (1)

Robert
Robert

Reputation: 36823

Note: this is how to run mysqld process as SO's root user, and not how to get the root MySQL user.

I don't know whether exists a better approach but this works.


Viewing the official entrypoint.sh, it seems that it has no support of chaging the default mysql user I realized how to run mysql as root but you need to have already initialized the data directory.

Step 1) Start a normal mysql in order to initialize a volume (the mysql entrypoint.sh will do that job):

docker run \
  --rm \
  -v $(pwd)/mysql/:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD="abc" \
mysql:5.6

Step 2) Stop and remove that container:

 docker stop <container-id>

Step 3) Start again a new mysql process based on the data dir that has been created, but this time avoid to run the official mysql entrypoint:

docker run \
  -v $(pwd)/mysql/:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD="abc" \
  --entrypoint mysqld \
mysql:5.6 \
  --user root

Step 4) Check it:

▶ docker exec -it 4add4d065c3e bash
root@4add4d065c3e:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  2.8 23.0 1314864 471104 ?      Ssl  15:12   0:00 mysqld --user root
root        28  3.0  0.1  20248  3040 ?        Ss   15:12   0:00 bash
root        34  0.0  0.1  17500  2068 ?        R+   15:12   0:00 ps aux

Upvotes: 1

Related Questions