How to Backup Mysql using mysqldump and gzip

Backup mysql in linux can be done by using mysqldump. The output of dumping is .sql file. We can combine mysqldump with gzip command to compressing the result.

Command for backup:

mysqldump -u username -p db_name | gzip -9 > db_name.sql.gz

Command for restore

zcat db_name.sql.gz | mysql -u username -p db_name

Another command can be use for backup and transfer the result using ssh to another server.

mysqldump --opt <database> | gzip -c | ssh user@otherserver 'cat > /tmp/yourdbname.sql.gz'

Source:

https://coderwall.com/p/ylpnvq/mysqldump-gzip-best-combo

https://serverfault.com/questions/106595/mysqldump-to-a-tar-gz

Enable Query Log in MySQL

Sometimes you want to know the queries executed on database server. MySQL Server provides a way to show the general query log and the slow query log, if those logs are enabled. Possible destinations for log entries are log files or the general_log and slow_log tables in the mysql system database. For those who use MySQL >= 5.1.1 2, you can control this option globally at runtime:

  1. Execute SET GLOBAL log_output = 'TABLE';
  2. Execute SET GLOBAL general_log = 'ON';
  3. Take a look at the table mysql.general_log

If you prefer to output to a file instead of a table:

  1. SET GLOBAL log_output = "FILE"; the default.
  2. SET GLOBAL general_log_file = "/path/to/your/logfile.log";
  3. SET GLOBAL general_log = 'ON';

What the advantage of using these command?

  1. It is not editing the my.cnf file and potentially permanently turning on logging
  2. You’re not fishing around the filesystem looking for the query log – or even worse, distracted by the need for the perfect destination. /var/log /var/data/log /opt /home/mysql_savior/var
  3. You don’t have to restart the server and interrupt any current connections to it.
  4. Restarting the server leaves you where you started (log is by default still off)

For more information, see MySQL 5.1 Reference Manual – Server System Variables – general_log

For those who want to  enable general log and slow query log and show them in tables in MySQL.

1. First, check if you already have the two tables slow_log and general_log existing in the mysql database.

If not, you have to create them, note that you have to create in the mysql database.

  • To create the general_log table:
  CREATE TABLE `general_log` (
   `event_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
                          ON UPDATE CURRENT_TIMESTAMP,
   `user_host` mediumtext NOT NULL,
   `thread_id` bigint(21) unsigned NOT NULL,
   `server_id` int(10) unsigned NOT NULL,
   `command_type` varchar(64) NOT NULL,
   `argument` mediumtext NOT NULL
  ) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log'

The general query log is a general record of what mysqld is doing. The server writes information to this log when clients connect or disconnect, and it logs each SQL statement received from clients. The general query log can be very useful when you suspect an error in a client and want to know exactly what the client sent to mysqld.

  • And the slow_log table
  CREATE TABLE `slow_log` (
   `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP 
                          ON UPDATE CURRENT_TIMESTAMP,
   `user_host` mediumtext NOT NULL,
   `query_time` time NOT NULL,
   `lock_time` time NOT NULL,
   `rows_sent` int(11) NOT NULL,
   `rows_examined` int(11) NOT NULL,
   `db` varchar(512) NOT NULL,
   `last_insert_id` int(11) NOT NULL,
   `insert_id` int(11) NOT NULL,
   `server_id` int(10) unsigned NOT NULL,
   `sql_text` mediumtext NOT NULL,
   `thread_id` bigint(21) unsigned NOT NULL
  ) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log'

The slow query log consists of SQL statements that take more than long_query_time seconds to execute and require at leastmin_examined_row_limit rows to be examined. The slow query log can be used to find queries that take a long time to execute and are therefore candidates for optimization.

The minimum and default values of long_query_time are 0 and 10, respectively. The value can be specified to a resolution of microseconds.

2. Enable Query logging on the database

SET global general_log = 1;
SET global log_output = 'table';

3. Now you can view the log by running this query:

SELECT
    *
FROM
    mysql.general_log;

4. If you want to disable query logging on the database, run this query:

SET global general_log = 0;


References:

https://stackoverflow.com/questions/650238/how-to-show-the-last-queries-executed-on-mysql
https://tableplus.com/blog/2018/10/how-to-show-queries-log-in-mysql.html

MySQL: Backup Database

Untuk membackup database MySQL dapat dilakukan menggunakan perintah berikut:

mysqldump --opt -u username -p nama-database > nama-file-backup

keterangan dari perintah diatas adalah sebagai berikut:

  • username: username dari pemilik database yang akan dibackup
  • nama-database : nama dari database yang akan dibackup
  • nama-file-backup: nama file hasil backup database tersebut

Berikut contoh pemakaian perintah diatas untuk database bernama db_mahasiswa dengan username pemilik depratama dan nama file backup dalam bentuk plain text adalah db_mahasiswa_backup:

mysqldump --opt -u depratama -p db_mahasiswa > db_mahasiswa_backup