Some useful knowledge for MySQL databases.
To backup smaller databases, automysqlbackup
is a good choice for a MySQl database backup. In this chapter, I show the procedure to setup it.
Following informations are quite important for backup purposes:
root
!mysql -e "show variables;" | grep '^datadir'
ls $(mysql -e "show variables;"|awk '/^datadir/ {print $NF}')/|grep 'bin.index'
If this command does not result with a filename, the binlog
option is not activated!
du -sh $(mysql -e "show variables;" | awk '/^datadir/ {print $NF}')
mysql -e "show variables;"|grep '^socket'
mysql -e "show variables;"|grep '^port'
Now, you are ready to edit the configuration file of automysqlbackup:
CONFIG_mysql_dump_username='root'
CONFIG_mysql_dump_password='<password>'
CONFIG_backup_dir='<backup dir>'
PREBACKUP=“<prebackup-script>”
POSTBACKUP=“<postbackup-script>”
The configuration file is expected here: /etc/automysqlbackup/automysqlbackup.conf
It's not a bad idea to test the configuration first. Just un comment the line containing this: CONFIG_dryrun=0
and change it to CONFIG_dryrun=1
. Just run the script: /usr/bin/automysqlbackup
.
If no error was listed, you can disable the CONFIG_dryrun=1
option in /etc/automysqlbackup/automysqlbackup.conf
There are several ways to restore a database dump file:
mysql <dbname> -e "source daily_<dbname>_2019-03-09_05h00m_Saturday.sql;"
bzip2 -cd daily_<dbname>_2019-03-09_05h00m_Saturday.sql.bz2 | mysql <dbname>
gzip -cd daily_<dbname>_2019-03-09_05h00m_Saturday.sql.gz | mysql <dbname>
In case, you have to create a backup manually (without automysqlbackup script), you can do it like this:
gzip
and backup all databases:mysqldump --all-databases -q | gzip -c > mysql-20100212.sql.gz
bzip2
and backup all databases:mysqldump --all-databases -q | bzip2 -c > mysql-20100212.sql.bz2
In this example, the password for the MySQL user root will be updated for following hosts: ::1
, 127.0.0.1
and localhost
.
mysql> UPDATE mysql.user SET Password=PASSWORD('<password>') WHERE User='<username>' AND Host='::1'; mysql> UPDATE mysql.user SET Password=PASSWORD('<password>') WHERE User='<username>' AND Host='127.0.0.1'; mysql> UPDATE mysql.user SET Password=PASSWORD('<password>') WHERE User='<username>' AND Host='localhost';
or, if you want to change the password for all hosts of this user:
mysql> UPDATE mysql.user SET Password=PASSWORD('<password>') WHERE User='<username>'
To get all user permissions and attributes for a user, following command is helpful:
mysql> SELECT * FROM mysql.user WHERE user='<username>' AND host='localhost' \G
The result:
*************************** 1. row *************************** Host: localhost User: <username> Password: *D115A249596ECD669091C1C6535DE65DE8427472 Select_priv: Y Insert_priv: Y Update_priv: Y Delete_priv: Y Create_priv: Y Drop_priv: Y Reload_priv: Y Shutdown_priv: Y Process_priv: Y File_priv: Y Grant_priv: Y References_priv: Y Index_priv: Y Alter_priv: Y Show_db_priv: Y Super_priv: Y Create_tmp_table_priv: Y Lock_tables_priv: Y Execute_priv: Y Repl_slave_priv: Y Repl_client_priv: Y Create_view_priv: Y Show_view_priv: Y Create_routine_priv: Y Alter_routine_priv: Y Create_user_priv: Y Event_priv: Y Trigger_priv: Y Create_tablespace_priv: Y ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: authentication_string: 1 row in set (0.01 sec)
If you want to get attributes for all hosts, just remove the AND host='localhost
' query in the WHERE
clause!