MariaDB, fork communautaire de MySQL qui a été racheté par Oracle, est un système de gestion de base de données sous licence GPL. La migration de MySQL vers MariaDB ne pose pas de problème particulier car MariaDB est basée sur le code source de MySQL.
Installation de MariaDB
Utilisation de la commande apt-get pour installer les paquets de MariaDB.
# apt install mariadb-server
Puis tapez cette commande pour finaliser la configuration :
# mysql_secure_installation
On vous demandera de rentrer votre mot de passe root (J’ai rien mis et c’est OK). Répondre ensuite Y à toutes les autres questions.
Vous donnerez ensuite un mot de passe à l’utilisateur root pour MariaDB, différent de l’utilisateur root du serveur. Cet utilisateur de la base de données aura tous les droits d’accès donc utilisation d’un mot de passe complexe.
Les connexions anonymes seront désactivées ainsi que les connexions root depuis un serveur différent.
# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.
Enter current password for root (enter for none): OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.
Set root password? [Y/n] Y New password: Re-enter new password: Sorry, passwords do not match.
New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? [Y/n] Y ... Success!
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y ... Success!
By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove test database and access to it? [Y/n] Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success!
Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
Reload privilege tables now? [Y/n] Y ... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB installation should now be secure.
Thanks for using MariaDB!
C’est terminé, MariaDB est maintenant installée.
Autoriser la connexion distante de root
Normalement root ne peu pas directement se connecter à MariaDB (sauf en local) car l’installation par défaut de MariaDB applique cette restriction de sécurité. Si vous tentez de vous connecter au serveur depuis un hôte distant vous aurez ce message d’erreur :
!: SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' sur une page web
Si vous êtes certain·e de vouloir autoriser cet accès distant alors connecter vous à mysql dans un premier temps.
# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 10.1.23-MariaDB-9+deb9u1 Debian 9.0
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Tapez ensuite la commande USE mysql; qui vous connectera à la base de données mysql contenant la configuration du serveur.
MariaDB [(none)]> USE mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed
Puis tapez la requête SELECT plugin FROM user WHERE user='root';. Vous devriez avoir cet affichage :
MariaDB [mysql]> SELECT plugin FROM user WHERE user='root'; +-------------+ | plugin | +-------------+ | unix_socket | +-------------+ 1 row in set (0.00 sec)
C’est à cause du plugin unix_socket que la connexion root est refusée. Pour remédier au problème, tapez la requête UPDATE user SET plugin='' WHERE User='root'; qui supprimera ce plugin pour l’utilisateur root.
MariaDB [mysql]> UPDATE user SET plugin='' WHERE User='root'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
Recharger les privilèges utilisateurs avec la commande FLUSH PRIVILEGES; puis quitter MariaDb avec EXIT;
Ensuite vous pouvez créer vos tables et les remplir manuellement ou utiliser un fichier .php qui le fait directement :
<?php
Print("<hr><b> Resultat </b>d'une requête SQL distante sur MariaDB<hr>"); try { $dbh = new PDO('mysql:host=172.17.0.3;dbname=testdb','root','admin'); // Creation d'une table et remplissage $dbh->query(' DROP TABLE IF EXISTS users; CREATE TABLE IF NOT EXISTS users ( nom text NOT NULL, age int(3) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; INSERT INTO users (nom, age) VALUES ("Parasite Inc.", 108), ("Amon Amarth", 82), ("Kalmah", 32), ("Månegarm", 39), ("Svartsot", 625);'); // Affichage du contenu de la table user foreach($dbh->query('SELECT * from users') as $row) { print_r($row); Print("</br>"); } } catch (PDOException $e) { print "Erreur !: " . $e->getMessage() . "<br/>"; die(); } phpinfo();
?>
Connexion depuis un hôte distant
Par exemple avec un framework comme PHPstorm. Par défaut, MySQL ou MariaDB n’écoute que les connexions de l’hôte local. Tous les accès à distance au serveur sont refussé. Pour activer l’accès à distance, exécutez les commandes ci-dessous pour ouvrir le fichier de configuration MySQL / MariaDB.
Pour Mysql :
# vim nano /etc/mysql/mysql.conf.d/mysql.cnf
Pour MariaDB server :
# vim nano /etc/mysql/mariadb.conf.d/50-server.cnf
# mysql -u root -pP4ssW0rD ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Si vous avez oublié votre mot de passe ou si vous souhaitez le changer :
1 / Stopper Mysql.
# service mysqld stop Stopping MySQL: [ OK ]
2 / redémarrer MySQL avec --skip-grant-tables.
# mysqld_safe --skip-grant-tables Starting mysqld daemon with databases from /var/lib/mysql
3 / Ouvrir un nouveau terminal et connectez-vous en root.
# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g.
4 / Changer de base de données.
mysql> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed
5 / Changer le mot de passe.
mysql> update user set password=password('root123') where user='root'; Query OK, 3 rows affected (0.00 sec) Rows matched: 3 Changed: 3 Warnings: 0
6 / flush.
mysql> flush privileges;
7 / Quitter.
mysql> quit Bye
8 / Restart.
# service mysqld restart; Stopping MySQL: [ OK ] Starting MySQL: [ OK ]
Vous devriez maintenant pouvoir vous connecter à mySQL avec votre nouveau mot de passe