Skip to main content

MariaDB users and permissions

Most database problems in the lab are not SQL problems. They are login and permission problems: a user missing, made for the wrong host, or with no rights. This page explains how MariaDB decides who gets in, and how to fix each case.

All commands here are typed inside MariaDB as an admin. Get there with:

sudo mariadb

MariaDB users are not Linux users​

Linux user pi and MariaDB user Nypuser have nothing to do with each other. MariaDB keeps its own list of users and passwords in its own database (mysql.user). Changing the pi password does not change any MariaDB password, and the other way round.

Root: unix_socket vs password​

MariaDB's root can log in two ways:

MethodHow you log inWorks from
unix_socketsudo mariadb (no password asked)The Pi's terminal only, as Linux root (sudo)
passwordmysql -u root -p + passwordTerminal; not phpMyAdmin when root is restricted to unix_socket

With unix_socket, MariaDB does not check a password at all. It asks Linux "is this process run by root?". That is why sudo mariadb works even when nobody remembers the root password, and why it is the way back in whenever you are locked out.

See how root is set up:

SELECT User, Host, plugin FROM mysql.user WHERE User='root';

unix_socket or mysql_native_password in the plugin column tells you which method is active. Allow both (so sudo mariadb keeps working and the password works too):

ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('admin');

The host part​

Every MariaDB account is user + host:

AccountWho can use it
'Nypuser'@'localhost'Programs on the Pi itself: phpMyAdmin, your Python, mysql in the terminal
'Nypuser'@'%'Any computer, including the Pi
'Nypuser'@'172.16.%'Only computers on the lab network
'Nypuser'@'127.0.0.1'The Pi itself, over TCP only (rarely what you want)

These are separate accounts with separate passwords and permissions. When several match, MariaDB uses the most specific host. So if both 'Nypuser'@'localhost' and 'Nypuser'@'%' exist, logging in on the Pi always uses the localhost one, and a password you changed on the % account seems not to work.

Always look before you fix:

SELECT User, Host, plugin FROM mysql.user ORDER BY User;

Tidy up a duplicate you do not want:

DROP USER 'Nypuser'@'localhost';

Create a user properly​

Three steps, in this order:

CREATE USER 'sensorapp'@'localhost' IDENTIFIED BY 'S3nsor!pass'; -- 1. the login
GRANT ALL PRIVILEGES ON MySensor.* TO 'sensorapp'@'localhost'; -- 2. what it may do
SHOW GRANTS FOR 'sensorapp'@'localhost'; -- 3. check

Then test it as that user, from the Linux prompt:

mysql -u sensorapp -p MySensor -e "SELECT COUNT(*) FROM Data;"

If that works, the same user, password and database will work in Python, Flask, PHP and phpMyAdmin.

Permissions (GRANT)​

You wantCommand
Everything, everywhere (superuser, like Nypuser in the lab)GRANT ALL PRIVILEGES ON *.* TO 'u'@'h' WITH GRANT OPTION;
Everything in one databaseGRANT ALL PRIVILEGES ON MySensor.* TO 'u'@'h';
Read and add rows in one table (what a sensor logger needs)GRANT SELECT, INSERT ON MySensor.Data TO 'u'@'h';
Read only (for a web page that just shows data)GRANT SELECT ON MySensor.* TO 'u'@'h';
Take a right awayREVOKE INSERT ON MySensor.Data FROM 'u'@'h';

Reading the level:

  • *.* = every table in every database (including MariaDB's own mysql database).
  • MySensor.* = every table in MySensor.
  • MySensor.Data = one table.
  • WITH GRANT OPTION = the user may give its rights to others. Only for admin accounts.

SHOW GRANTS showing only GRANT USAGE ON *.* TO ... means the user can log in but has no rights at all: it will see no databases.

Passwords​

ALTER USER 'Nypuser'@'%' IDENTIFIED BY 'newpassword';

Forgot every password? sudo mariadb still gets you in as root (unix_socket), and from there you can reset any user's password.

If sudo mariadb also says Access denied, root was switched to password-only. Start MariaDB without permission checks for a moment:

sudo systemctl stop mariadb
sudo mysqld_safe --skip-grant-tables --skip-networking &
mariadb -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('admin');
exit
sudo mysqladmin shutdown
sudo systemctl start mariadb

FLUSH PRIVILEGES comes first here: in skip-grant-tables mode it loads the user tables so ALTER USER works. --skip-networking stops anyone else connecting while there are no passwords.

Mistakes people make with users​

MistakeWhat you seeFix
Logging in as root with -p as user pi when root uses unix_socketERROR 1698 Access denied for user 'root'@'localhost'sudo mariadb, or allow both methods (above)
User made for % only, plus an old localhost copy with another passwordPassword "does not work" on the PiCheck SELECT User, Host ..., drop or fix the duplicate
User created, never grantedLogin works, no databases visibleGRANT ... ON MySensor.* ...
GRANT ... ON MySensor (no .*)ERROR 1046 No database selected, or the rights go to a table named MySensorUse MySensor.*
Different capitals (nypuser, mysensor)1045, 1049 Unknown databaseNames are case-sensitive on Linux
Granting to a user that does not existERROR 1133 Can't find any matching row in the user tableCREATE USER first, exactly the same name and host
Creating a user that already existsERROR 1396 Operation CREATE USER failedIt exists: ALTER USER to change the password, or DROP USER first
Using root in Python/PHP codeWorks, but a leaked script exposes everythingMake one user per program with only the rights it needs
Editing mysql.user with UPDATEChanges do nothing until FLUSH PRIVILEGES, or break the accountAlways use CREATE/ALTER/DROP USER and GRANT

FLUSH PRIVILEGES is only needed after editing the user tables directly. After GRANT or CREATE USER it is not needed, but it does no harm.

Connecting from another computer​

A 'Nypuser'@'%' account is only half of it. Out of the box MariaDB listens only on the Pi itself:

sudo ss -tlnp | grep 3306 # 127.0.0.1:3306 = local only

To accept connections from the lab network:

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf # bind-address = 127.0.0.1 -> 0.0.0.0
sudo systemctl restart mariadb

Then connect from a laptop with mysql -h 172.16.x.x -u Nypuser -p (or a tool such as HeidiSQL or DBeaver). Only do this on the lab network: it lets any computer that can reach the Pi try to log in.

ERROR 1130 Host '172.16.x.x' is not allowed to connect = MariaDB is listening, but there is no account for that host. Create the user with % or that network.