{"id":550,"date":"2024-08-16T09:06:40","date_gmt":"2024-08-16T13:06:40","guid":{"rendered":"https:\/\/waterloobae.com\/?p=550"},"modified":"2025-08-20T14:27:10","modified_gmt":"2025-08-20T18:27:10","slug":"mysql-backup-restore-import-and-export","status":"publish","type":"post","link":"https:\/\/waterloobae.com\/blog\/2024\/08\/16\/mysql-backup-restore-import-and-export\/","title":{"rendered":"[MySQL] Backup, Restore, Import, and Export"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When needed, snapshot backups of MySQL instance are not that useful. So, I tend to backup my DB with mysqldump. The script below dump all of schemas including system tables, users and privileges. It also drops table if exists and add them with its data. This script saved my days so often.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">#!\/bin\/bash\n<strong>sudo mysqldump -A -Y --master-data=2 --add-drop-table --lock-all-tables -uroot -p &gt; mybackup.sql<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">One DB\/Schema can be exported\/backed up as below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">#!\/bin\/bash\n<strong>mysqldump --add-drop-table --databases db-name -u root -p &gt; mybackup.sql<\/strong><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Upgrading MySQL  <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you try to import old MySQL dump into the new MySQL, you often can get <em>\u2018mysql.innodb_index_stats\u2019 doesn\u2019t exist in engine<\/em> and more. There are two solutions for that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First of all, you can ignore innodb index and table when dumping. as below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">#!\/bin\/bash\n<strong>sudo mysqldump -A -Y --master-data=2 --add-drop-table --lock-all-tables --ignore-table=mysql.innodb_index_stats --ignore-table=mysql.innodb_table_stats -uroot -p &gt; mybackup.sql<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Or you can dump the whole instance and import as below. Please note that <strong>&#8220;-f&#8221;<\/strong> is the magic word here.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"> [sudo] mysql -u root -p -f &lt; sample.sql<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">DB backup senario<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dump files are created from log files. So, it is possible to perform full backup and incremental backup by flushing log files as planned.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The script below performs a full backup every Sunday and it flushes logs and delete master logs to make it sure next backup won&#8217;t overlaps with this one. Other days, it just flushes logs only when exporting daily incremental backup. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">#!\/bin\/bash\nif [ \"`date +%A`\" == \"Sunday\" ]; then\n\n\tmysqldump -A -Y --flush-logs --delete-master-logs --master-data=2 --add-drop-table --lock-all-tables -uroot -pPassword &gt; mybackup.sql\n\nelse\n\n        today=`date +%A`\n        logno=`ls -d \/var\/log\/mysql\/mysql-bin.?????? | sed 's\/^.*\\.\/\/' | sort -g | tail -n 1`\n        mysqladmin -uroot -pPassword flush-logs\n        mysqlbinlog \/var\/log\/mysql\/mysql-bin.$logno &gt; $today.sql\n\nfi<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up mint MySQL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After installation, you can log in MySQL. It is the most crucial task to set up root user correctly. Otherwise, you won&#8217;t be able to connect with DB remotely at all. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First login<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"># This will log you in as user root.\nsudo mysql <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, create root user with password<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">CREATE USER 'root'@'localhost' IDENTIFIED BY 'YourPassword';\nCREATE USER 'root'@'%' IDENTIFIED BY 'YourPassword'; <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">MySQL documents say &#8216;root&#8217;@&#8217;%&#8217; will create &#8216;root&#8217;@&#8217;localhost&#8217; as well. However, it is safer to run both of them. You can give different password to make your credentials more secure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then, Grant privileges<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;\nGRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;\nFLUSH PRIVILEGES; <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For MySQL Server initial installation, you can google them. Just be cautious not to follow wrong instructions. I found <a href=\"https:\/\/medium.com\/@jasonrbodie\/how-to-install-mysql-on-ubuntu-24-04-bfd11f73224b\">this one<\/a> is quite updated and useful. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When needed, snapshot backups of MySQL instance are not that useful. So, I tend to backup my DB with mysqldump. The script below dump all of schemas including system tables, users and privileges. It also drops table if exists and add them with its data. This script saved my days so often. One DB\/Schema can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,4],"tags":[],"class_list":["post-550","post","type-post","status-publish","format-standard","hentry","category-database","category-it-tips"],"_links":{"self":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/posts\/550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/comments?post=550"}],"version-history":[{"count":9,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/posts\/550\/revisions"}],"predecessor-version":[{"id":592,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/posts\/550\/revisions\/592"}],"wp:attachment":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/media?parent=550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/categories?post=550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/tags?post=550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}