====== MySQL ====== ===== Installation ===== root@serveur:/home/simon# apt install mariadb-server ===== Sécurité ===== Après l'installation, il faut utiliser "mysql_secure_installation" pour mettre un mot de passe root et sécuriser un minimum MySQL ou MariaDB. root@serveur:/home/simon# 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] 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] ... 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] ... 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] - 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] ... 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! ===== Quelques exemples ===== Se connecter : $ mysql -u root -p Enter password: (...) Voir les base de données existantes : mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | (...) | +--------------------+ Voir les tables dans une base de données : mysql> show tables in nextcloud; +-----------------------------+ | Tables_in_nextcloud | +-----------------------------+ | oc_accounts | | oc_activity | | (...) | +-----------------------------+ Créer une base de données : mysql> CREATE DATABASE nom-de-la-db; Query OK, 1 row affected (0.00 sec) Créer un nouvel utilisateur pour une base de données : mysql> CREATE USER '[USER]'@'localhost' IDENTIFIED BY '[PASSWORD]'; Query OK, 0 rows affected (0.00 sec) Donner les droits à un utilisateur pour une base de données : mysql> GRANT ALL PRIVILEGES ON [nom_DB].* TO '[USER]'@'localhost' IDENTIFIED BY '[PASSWORD]'; Query OK, 0 rows affected (0.01 sec) Supprimer une base de données : mysql> drop database 'nom_DB'; ===== mysqldump ===== ==== Faire des "dump" ==== Une seule base de données : # mysqldump -u root -p database1 > /tmp/database1.sql Plusieurs tables dans une base de données : $ mysqldump -h 192.168.1.12 -u root -p basededonnees table1 table2 > dump-table1-table2.sql La __structure__ de données d'une table (avec l'argument **-d** ou **--no-data**) : $ mysqldump -d -h 192.168.1.14 -u root -p basededonnees > dump-basededonnees.sql Plusieurs bases de données : # mysqldump -u root -p --databases databaseb1 database2 database3 > /tmp/all-database.sql Enter password: Rajouter la date dans le dump : $ mysqldump -h HOST -u root -p NOM_DB | bzip2 > `date "+%Y-%m-%d"`-NOM-DB.sql.bz2 Vérification : # grep " Database:" /tmp/all-database.sql -- Host: localhost Database: database1 -- Current Database: `database1` -- Current Database: `database2` -- Current Database: `database3` ==== Restaurer ==== On peut créer une base de données (pour tester), la sauvegarder et puis la supprimer pour enfin essayer de la restaurer. Pour supprimer la DB, on utilise "drop": mysql root@localhost:(none)> drop DATABASE database1; Si on essaye de restaurer la base de données, sans la recréer au préalable, on a une erreur disant qu'elle n'existe pas. On commence donc par créer la base de données : mysql root@localhost:(none)> CREATE DATABASE database1; Pour restaurer, on utilise **mysql** (et non mysqldump!!) : # mysql -u root -p database1 < /tmp/database1.sql Enter password: ===== Outils annexes ===== ==== mycli ==== mycli permet d'avoir une invite de commande dans mysql qui soit colorée et avec de l'auto-complétion. # apt-get install python3-dev python3-pip Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: libpython3-dev libpython3.4 libpython3.4-dev python3-chardet python3-colorama python3-distlib python3-html5lib python3-requests python3-setuptools python3-six python3-urllib3 python3-wheel python3.4-dev (...) # pip3 install mycli ==== MySQLTuner ==== MySQLTuner analyse les requêtes SQL et propose des améliorations pour optimiser la base de données. Github [[https://github.com/major/MySQLTuner-perl|MySQLTuner-perl (github)]] ===== Sources ===== * [[https://blog.adminrezo.fr/2016/01/mycli-pgcli-mysql-postregsql-clients/|mycli (adminrezo)]]