Gebruikers (MySQL): verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
 
(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>)
  
== Gebruiker aanmaken - Basis ==
+
== See all users ==
 +
 
 +
<pre>
 +
select host, user from mysql.user;
 +
</pre>
 +
 
 +
== Create user ==
 +
 
 +
Not very exciting:
  
 
<pre>
 
<pre>
Regel 17: Regel 26:
 
</pre>
 
</pre>
  
* Als je dit commando uitvoert terwijl de user al bestaat, krijg je een foutmelding!
+
* If you run this command when the user already exists, you will get an error message
* Alleen het wachtwoord moet tussen apostrophes staan. Misschien dat je dat ook moet doen voor <code>username</code> als je tekens moet ''escapen'', maar dat heb ik nooit
+
* The password must be between apostrophes
* Deze gebruiker heeft nog geen rechten - Dat doen we hieronder.
+
* 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 ==
  
== Rechten toekennen aan gebruiker - Basis ==
+
Usual syntax to grant rights to a user:
  
 
<pre>
 
<pre>
grant all on mijn_db.* to username@localhost;
+
grant all on my_db.* to username@localhost;
 
</pre>
 
</pre>
  
* <code>mijn_db.*</code> - Alle objecten van de betreffende database
+
* <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
  
FLUSH PRIVILEGES;
+
<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:
  
grant all on mijn_db.* to mijn_user@localhost identified by 'mijn_wachtwoord';
+
<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-account ==
+
=== Beheerder-rechten ===
  
Om beheerder-accounts aan te maken, moet je de ''grant-privilege'' apart benoemen. Bv.:
+
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>
  
grant all on *.* to jeroen@localhost identified by 'MijnGeheimeWachtwoord' with grant option;
+
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>
 
 
== grant-privileges aan een gebruiker toekennen ==
 
 
Wat in ieder geval werkt (ingelogd via <code>sudo mysql</code>):
 
 
<pre>
 
create user jeroen@localhost identified by 'MijnWachtwoord';
 
grant all on *.* to 'jeroen'@'localhost' with grant option;
 
</pre>
 
 
En om het zeker te weten:
 
 
<pre>
 
select user, grant_priv from mysql.user;
 
 
</pre>
 
</pre>
  
Regel 127: Regel 155:
 
Zie ook [[Configuratiebestanden (MySQL)]].
 
Zie ook [[Configuratiebestanden (MySQL)]].
  
== Probleem met inloggen in een verse instantie? (mei 2018) ==
+
== 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 ==
  
* De routine tijdens installatie van een MySQL-database, doet het niet goed. Oa. het aanmaken van een gebruiker werkt niet. In dat geval kun je in ieder geval als root inloggen middels <code>sudo mysql</code>
+
E.g.:
* Met <code>select Host, User from mysql.user;</code> kun je zien of het betreffende account wel of niet is aangemaakt. Voorbeeld:
 
  
 
<pre>
 
<pre>
select Host, User from mysql.user;
+
select host, user from mysql.user;
+-----------+------------------+
+
 
| Host      | User             |
+
drop user en_s1;             # Error: No host specified
+-----------+------------------+
+
drop user en_s1@localhost;  # OK
| localhost | debian-sys-maint |
 
| localhost | kbb3            |
 
| localhost | kbb_2_          |
 
| localhost | kbb_2_2          |
 
| localhost | mysql.session    |
 
| localhost | mysql.sys        |
 
| localhost | root            |
 
+-----------+------------------+
 
 
</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

MySQL-rechten volgens Worfence - Handig!

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

Zie ook

Bronnen