Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

mysqld stops with 'Please read Security section ... run mysqld as root'

ErrorFatal error: Please read "Security" section of the manual to find out how to run mysqld as root!

Short answer

mysqld aborts when the process is root and no --user option is given. Run it as an unprivileged account instead: start the service through systemd as the mysql user, or add user=mysql to the [mysqld] group of my.cnf. Make sure that user owns the data directory.

  • Applies to MySQL 8.0, MySQL 8.4, Linux
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which operating system user starts the server and whether a user= or --user setting exists.

    ps -o user,cmd -C mysqld
    grep -rn "^user" /etc/my.cnf /etc/mysql 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1You ran mysqld or a custom start script as root (or with sudo), and the log shows the fatal message at once.mysqld was started as root with no --userFix
    2. 2The service worked before a manual edit or a tar.gz install, and starts as root at boot.The option file has no user= lineFix
    3. 3The root error is gone, but mysqld now fails to read or create files in the data directory.The data directory belongs to root after you switched userFix
    4. 4You installed from the MySQL repository, but start the server by hand as another user.A package init script is used with a different accountFix

What does the error look like?

  • Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!

Cause 1 of 4

mysqld was started as root with no --user

1Check

The server code checks the effective user id. If it is 0 and no --user is set, it logs the error and exits (initialisation and help modes are exempt).

id -un

2Fix

Give mysqld the account to switch to. It starts as root, then switches before accepting connections.

sudo mysqld --user=mysql

3Verify

The server stays up and the process owner is the mysql user.

ps -o user,cmd -C mysqld

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 4

The option file has no user= line

1Check

The manual lets you make the choice permanent in the [mysqld] group of /etc/my.cnf or the my.cnf of the data directory.

grep -n -A3 "\[mysqld\]" /etc/my.cnf

2Fix

Add the user line, then restart.

[mysqld]
user=mysql

3Verify

Restart and read the process owner.

sudo systemctl restart mysql
ps -o user,cmd -C mysqld

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 4

The data directory belongs to root after you switched user

1Check

When the server runs as another user, that user must own the data directory or it cannot open databases or tables.

ls -ld /var/lib/mysql

2Fix

Stop the server, give the data directory to the account, and start again. Links inside it may need their targets changed too.

sudo mysqladmin shutdown
sudo chown -R mysql /var/lib/mysql

3Verify

Start the service and run a query.

sudo systemctl start mysql
mysql -e "SELECT 1"

Confirmed by dev.mysql.com

Did this fix it?

Cause 4 of 4

A package init script is used with a different account

1Check

For repository or RPM installs, the manual says the init scripts expect the local mysql user and do not support another user.

getent passwd mysql

2Fix

Use the packaged service (systemctl) rather than a hand-written command, and keep the account named mysql.

sudo systemctl start mysql

3Verify

Check the service state.

systemctl status mysql --no-pager

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Never run the database as root; the manual advises against it.
  • Start it only through the service manager.
  • After restoring a data directory from a copy, set its owner before the first start.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.