Gebruikers (MySQL)
Wat ik meestal zoek:
create database mw collate utf8_general_ci; drop user if exists username@localhost; create user username@localhost identified by 'password'; grant all on mw.* to username@localhost;
- Merk op dat alleen het wachtwoord omgeven is door apostrophes
- Sinds een update uit 2018, kun je binnen de MySQL-client met 'pijltje omhoog' dit commando niet terughalen vanwege beveiliging
- Sinds een update uit 2020, krijg ik foutmeldingen als ik de laatste twee commando's samenvoeg in één (met zoiets als
grant all on mw.* to username@localhost identified by 'password';
)
See all users
select host, user from mysql.user;
Create user
Not very exciting:
create user username@localhost identified by 'password';
- If you run this command when the user already exists, you will get an error message
- The password must be between apostrophes
- When
username
needs to be escaped, you also need to include it in apostrophes - I've never had such a situation - Probably to be on the safe side: Include all three identities in apostrophes? Just as MySQL WorkBenech includes table names always in apostrophes.
Recreate a user
Want to recreate a user, for example because you don't remember the password?
drop user if exists username@localhost; create user username@localhost identiefied by 'GeheimWachtwoord';
Assign rights
Usual syntax to grant rights to a user:
grant all on my_db.* to username@localhost;
my_db.*
- Alle objecten van de betreffende database- Zoals je hieronder bij rechten kunt zien, heb je al snel create en drop nodig. Vandaar dat je snel belandt bij all.
Om dit te verifiëren:
select user, grant_priv from mysql.user;
Verschillende rechten
Er zijn tientallen verschillende rechten [1]. Wellicht de belangrijkste:
- CREATE - Databases én tabellen aanmaken
- DROP - Databases én tabellen verwijderen
- INSERT - Rijen toevoegen aan een database
- SELECT - select-queries uitvoeren
- UPDATE - Rijen bijwerken
- GRANT OPTION - Beheer van privileges van andere gebruikers.
Deze rechten kun je meestal per database, per tabel, en op nog een derde niveau (effe vergeten wat precies) inregelen. Op tabel-niveau doe je dit door iets in te vullen voor het '*' in mijn_db.*
.
Flush privileges
Na het toekennen van rechten, moet je die activeren middels
flush privileges;
Meestal heb ik dat niet nodig. Misschien alleen als je iemand beheerrechten toekent?
Website-database
In situaties dat ik per website een apart MySQL-gebruikersaccount aanmaak, doe ik dat meestal met zoiets als dit:
grant all on mijn_db.* to mijn_user@localhost identified by 'mijn_wachtwoord';
Vermoedelijk kan dit fijnmaziger. Bv. wel drop-privilege op tabel-niveau, maar niet op database-niveau. Zie afbeelding hiernaast.
Beheerder-rechten
Om beheerder-rechten aan een gebruiker toe te kennen, moet je de grant-privilege apart benoemen. Bv.:
grant all on *.* to jeroen@localhost with grant option;
Complete procedure to create an admin account:
create user jeroen@localhost identified by 'password'; grant all on *.* to jeroen@localhost with grant option; flush privileges;
Rechten inzien
De rechten per gebruiker worden bijgehouden in tabel mysql.user
. Deze tabel is meestal te groot om goed te kunnen zien via de mysql-client. Wellicht handiger:
select host, user from mysql.user; +-----------+------------------+ | Host | User | +-----------+------------------+ | localhost | debian-sys-maint | | localhost | kbb3 | | localhost | kbb_2_ | | localhost | kbb_2_2 | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+------------------+
Inloggegevens onthouden
Godzijdank is er een methode waardoor je niet meer steeds je wachtwoord hoeft in te voeren bij inloggen. Dankzij aangepaste bestandspermissies, valt het beveiligingsprobleem ook mee:
Create a file named .my.cnf in your home directory that looks like this. Make sure the filesystem permissions are set such that only the owning user can read it (0600).
Voorbeeld-bestand .my.cnf
:
[client] host = localhost user = username password = thepassword socket = /var/run/mysqld/mysqld.sock #database = mysql
Bestandsrechten aanpassen:
chmod 0600 .my.cnf
Zie ook Configuratiebestanden (MySQL).
Problem logging into a fresh instance? (May 2018)
- The routine during installation of a MySQL database does not work properly. Oa. creating a user does not work. In that case you can at least log in as root using
sudo mysql
- With
select Host, User from mysql.user;
you can see whether the account in question has been created or not → See at the beginning of this article for an example.
Remove user
E.g.:
select host, user from mysql.user; drop user en_s1; # Error: No host specified drop user en_s1@localhost; # OK