How to Manage MariaDB User Accounts and Privileges
MariaDB identifies an account by its 'user'@'host' pair. Create the account with CREATE USER, assign the privileges it needs with GRANT, and verify the result with SHOW GRANTS and a separate connection. This walkthrough creates a read-only practice account, changes its name and password, then revokes its access and removes the account.
Choose the account name and connection host
Two entries with the same user name and different hosts are separate accounts. The Host component describes the connection source as MariaDB sees it. The practice account uses 127.0.0.1 for a TCP connection from the same machine.
| Host | Matching connections |
|---|---|
localhost |
Local connections. On Linux, this matches Unix socket connections. |
127.0.0.1 |
TCP connections from the IPv4 loopback address. |
% |
Wildcard that can match many sources. Omitting the host uses this value. |
For a remote application account, specify the application’s source IP or host name as seen by the server. If multiple entries match, MariaDB can prefer a more specific host. See the CREATE USER reference. Use a separate local MariaDB instance with an administrative connection, and create a fresh practice database with two sample rows.
CREATE DATABASE account_lab;
CREATE TABLE account_lab.reports (
report_id INT PRIMARY KEY,
label VARCHAR(30) NOT NULL
);
INSERT INTO account_lab.reports (report_id, label)
VALUES (1, 'Weekly'), (2, 'Monthly');
List and create user accounts
In the admin session, query the User and Host columns of mysql.user. This query checks just the names used below. If the account and host pair you intend to use already exists, choose a different practice name and use it consistently in the later commands.
SELECT User, Host
FROM mysql.user
WHERE User IN ('report_reader', 'report_viewer')
ORDER BY User, Host;
Replace REPLACE_WITH_UNIQUE_PASSWORD with a unique password for the practice account. Keep real passwords out of shared notes and query history.
CREATE USER 'report_reader'@'127.0.0.1'
IDENTIFIED BY 'REPLACE_WITH_UNIQUE_PASSWORD';
Creating an existing user and host pair usually returns ERROR 1396. IF NOT EXISTS reports a warning and leaves the account as it is. OR REPLACE drops and recreates it, which can discard privileges. Use ALTER USER to change an existing account.
Grant read access and test from a separate connection
Reading the reports requires SELECT on account_lab.reports. The scope account_lab.* covers the whole database, and *.* is global. Grant just the table access needed here. See the GRANT reference.
GRANT SELECT ON account_lab.reports TO 'report_reader'@'127.0.0.1';
SHOW GRANTS FOR 'report_reader'@'127.0.0.1';
Check that SHOW GRANTS lists the SELECT grant on account_lab.reports. A USAGE line by itself adds no data access. The output can contain authentication information, so keep it private.
Leave the admin session open and connect as the practice account in a second terminal. Substitute the actual practice server port if it differs from 3306. Enter the password at the prompt.
mariadb --host=127.0.0.1 --port=3306 --user=report_reader --password
Run these two statements in the practice account’s session. CURRENT_USER() identifies the account MariaDB uses for privilege checks.
SELECT CURRENT_USER();
SELECT report_id, label FROM account_lab.reports ORDER BY report_id;
The account should be report_reader@127.0.0.1 and the query should return 1 / Weekly and 2 / Monthly. An attempt to insert a row into the same table is denied because the account received only SELECT.
Change the account name and password
Close the practice account connection and return to the admin session. RENAME USER changes the user or host while preserving privileges. Verify that the destination user and host pair is available. The ALTER USER example below applies to this password-based practice account.
RENAME USER 'report_reader'@'127.0.0.1'
TO 'report_viewer'@'127.0.0.1';
ALTER USER 'report_viewer'@'127.0.0.1'
IDENTIFIED BY 'REPLACE_WITH_NEW_UNIQUE_PASSWORD';
SHOW GRANTS FOR 'report_viewer'@'127.0.0.1';
Replace the second placeholder with a different unique password. For accounts that use a different authentication plugin or multiple methods, use the syntax appropriate to their existing setup. Update the application’s connection settings, then use report_viewer and the new password in a fresh connection to confirm the two rows are still readable.
Revoke the privilege
In the admin session, use REVOKE to remove the table-level SELECT. Match the account and scope used in the grant.
REVOKE SELECT ON account_lab.reports FROM 'report_viewer'@'127.0.0.1';
SHOW GRANTS FOR 'report_viewer'@'127.0.0.1';
The account can still log in, but its reports query in a new connection should return ERROR 1142 in this exercise. If it still works, check whether a role or broader grant such as account_lab.* or *.* independently supplies SELECT.
Drop the account and verify new logins fail
When the account is no longer needed, close any application pool connections and user sessions first. Run DROP USER to remove the account and its grants. IF EXISTS avoids an error if the account has already been removed.
DROP USER IF EXISTS 'report_viewer'@'127.0.0.1';
Run the account list again as the administrator and verify that a new login as report_viewer fails. Dropping the account leaves the two rows in account_lab.reports. Before dropping a production account, check which applications use it and whether views or stored routines name it as their DEFINER.



