Gebruikers (MySQL): verschil tussen versies
(21 tussenliggende versies door dezelfde gebruiker niet weergegeven) | |||
Regel 3: | Regel 3: | ||
<pre> | <pre> | ||
create database mw collate utf8_general_ci; | create database mw collate utf8_general_ci; | ||
+ | drop user if exists username@localhost; | ||
create user username@localhost identified by 'password'; | create user username@localhost identified by 'password'; | ||
grant all on mw.* to username@localhost; | grant all on mw.* to username@localhost; | ||
Regel 11: | Regel 12: | ||
* Sinds een update uit 2020, krijg ik foutmeldingen als ik de laatste twee commando's samenvoeg in één (met zoiets als <code>grant all on mw.* to username@localhost identified by 'password';</code>) | * Sinds een update uit 2020, krijg ik foutmeldingen als ik de laatste twee commando's samenvoeg in één (met zoiets als <code>grant all on mw.* to username@localhost identified by 'password';</code>) | ||
− | == | + | == See all users == |
+ | |||
+ | <pre> | ||
+ | select host, user from mysql.user; | ||
+ | </pre> | ||
+ | |||
+ | == Create user == | ||
+ | |||
+ | Not very exciting: | ||
<pre> | <pre> | ||
Regel 17: | Regel 26: | ||
</pre> | </pre> | ||
− | * | + | * If you run this command when the user already exists, you will get an error message |
− | * | + | * The password must be between apostrophes |
− | + | * When <code>username</code> 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? | ||
+ | |||
+ | <pre> | ||
+ | drop user if exists username@localhost; | ||
+ | create user username@localhost identiefied by 'GeheimWachtwoord'; | ||
+ | </pre> | ||
+ | |||
+ | == Assign rights == | ||
− | + | Usual syntax to grant rights to a user: | |
<pre> | <pre> | ||
− | grant all on | + | grant all on my_db.* to username@localhost; |
</pre> | </pre> | ||
− | * <code> | + | * <code>my_db.*</code> - 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''. | * Zoals je hieronder bij ''rechten'' kunt zien, heb je al snel ''create'' en ''drop'' nodig. Vandaar dat je snel belandt bij ''all''. | ||
− | == Verschillende rechten == | + | Om dit te verifiëren: |
+ | |||
+ | <pre> | ||
+ | select user, grant_priv from mysql.user; | ||
+ | </pre> | ||
+ | |||
+ | === Verschillende rechten === | ||
Er zijn tientallen verschillende rechten [https://dev.mysql.com/doc/refman/8.0/en/grant.html]. Wellicht de belangrijkste: | Er zijn tientallen verschillende rechten [https://dev.mysql.com/doc/refman/8.0/en/grant.html]. Wellicht de belangrijkste: | ||
Regel 43: | Regel 70: | ||
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 <code>mijn_db.*</code>. | 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 <code>mijn_db.*</code>. | ||
− | == Flush privileges == | + | === Flush privileges === |
Na het toekennen van rechten, moet je die activeren middels | Na het toekennen van rechten, moet je die activeren middels | ||
− | + | <pre> | |
+ | flush privileges; | ||
+ | </pre> | ||
− | == Website-database == | + | Meestal heb ik dat niet nodig. Misschien alleen als je iemand beheerrechten toekent? |
+ | |||
+ | === Website-database === | ||
[[file:20180508-0934.png|thumb|MySQL-rechten volgens Worfence - Handig!]] | [[file:20180508-0934.png|thumb|MySQL-rechten volgens Worfence - Handig!]] | ||
Regel 55: | Regel 86: | ||
In situaties dat ik per website een apart MySQL-gebruikersaccount aanmaak, doe ik dat meestal met zoiets als dit: | In situaties dat ik per website een apart MySQL-gebruikersaccount aanmaak, doe ik dat meestal met zoiets als dit: | ||
− | + | <pre> | |
+ | grant all on mijn_db.* to mijn_user@localhost identified by 'mijn_wachtwoord'; | ||
+ | </pre> | ||
Vermoedelijk kan dit fijnmaziger. Bv. wel drop-privilege op tabel-niveau, maar niet op database-niveau. Zie afbeelding hiernaast. | Vermoedelijk kan dit fijnmaziger. Bv. wel drop-privilege op tabel-niveau, maar niet op database-niveau. Zie afbeelding hiernaast. | ||
− | == Beheerder- | + | === Beheerder-rechten === |
− | Om beheerder- | + | Om beheerder-rechten aan een gebruiker toe te kennen, moet je de ''grant-privilege'' apart benoemen. Bv.: |
+ | |||
+ | <pre> | ||
+ | grant all on *.* to jeroen@localhost with grant option; | ||
+ | </pre> | ||
− | + | Complete procedure to create an admin account: | |
− | == Rechten inzien == | + | <pre> |
+ | create user jeroen@localhost identified by 'password'; | ||
+ | grant all on *.* to jeroen@localhost with grant option; | ||
+ | flush privileges; | ||
+ | </pre> | ||
+ | |||
+ | === Rechten inzien === | ||
De rechten per gebruiker worden bijgehouden in tabel <code>mysql.user</code>. Deze tabel is meestal te groot om goed te kunnen zien via de mysql-client. Wellicht handiger: | De rechten per gebruiker worden bijgehouden in tabel <code>mysql.user</code>. Deze tabel is meestal te groot om goed te kunnen zien via de mysql-client. Wellicht handiger: | ||
Regel 82: | Regel 125: | ||
| localhost | root | | | localhost | root | | ||
+-----------+------------------+ | +-----------+------------------+ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</pre> | </pre> | ||
Regel 127: | Regel 155: | ||
Zie ook [[Configuratiebestanden (MySQL)]]. | 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 <code>sudo mysql</code> | ||
+ | * With <code>select Host, User from mysql.user;</code> 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.: | |
− | |||
<pre> | <pre> | ||
− | select | + | select host, user from mysql.user; |
− | + | ||
− | + | drop user en_s1; # Error: No host specified | |
− | + | drop user en_s1@localhost; # OK | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</pre> | </pre> | ||
Regel 150: | Regel 174: | ||
* [[Configuratiebestanden (MySQL)]] | * [[Configuratiebestanden (MySQL)]] | ||
+ | * [[Mysql secure installation]] | ||
== Bronnen == | == Bronnen == |
Huidige versie van 16 apr 2024 om 16:53
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