How to backup MySQL database on Windows? - mysql

I have WampServer 2.0 that is installed on Windows on my laptop.
I'm running on it an application that I wrote. The application is working with MySQL database.
I would like to make backups of this database periodically.
How this can be done ?
How could I define cron on Windows ?

The rough equivalent of crontab -e for Windows is the at command, as in:
at 22:00 /every:M,T,W,Th,F C:\path\to\mysql\bin\mysqldump.exe ...
Running the at command by itself lists the tasks you've created using at.
The mysqldump documentation is here.

The most popular way to backup MySQL database is to use mysqldump:
Open a Windows command line.
Specify the directory to mysqldump utility
cd "C:\Program Files\MySQL\MySQL Server 5.7\bin"
Create a dump of your MySQL database.
mysqldump.exe --user=YourUserName --password=YourPassword --host=localhost --port=3306 --result-file="Pathdump.sql" --databases "DatabaseName"
Also, there are a lot of third-party tools, which can perform MySQL backups automatically on a regular basis.

You could use a bash script.
#!/bin/sh
mysqldump -uroot -ppwd --opt db1 > /sqldata/db1.sql
mysqldump -uroot -ppwd --opt db2 > /sqldata/db2.sql
cd /sqldata/
tar -zcvf sqldata.tgz *.sql
cd /scripts/
perl emailsql.pl
http://paulbradley.tv/38/

Related

How to download sql database (apache server) to local using terminal?

I'm looking for downloading my sql database from a remote server to my local machine.
I used scp command like that :
scp -r user#ip:/var/lib/mysql/mydatabase .
It works, but I have lot of files (related to tables in my DB I suppose) : db.opt, files in .frm, .MYD, and .MYI
I tryed to copy my files to a new database folder in MAMP (to test the db), but when I watch my db in PhpMyAdmin I can see only one table :/
Is there a better way to download directly a sql database from server to local with terminal ?
Thanks a lot for your help guys
I suggest you use command, mysqldump --all-databases.
Over ssh work like this:
ssh user#ip mysqldump --all-databases > dump.sql
Download the dump with scp, and restore it whit php-myadmin.
simply use the mysqldump command over ssh, and redirect the output, so, you will have a dump of the DB
something like
ssh user#ip 'mysqldump -u dbuser -pdbassword database_name' > database_dump.sql
The you can restore the database using mysql on your local machine:
mysql -uusername -ppassword db_name < database_dump.sql
If the database is big, you can pipe a gzip like below:
ssh user#ip 'mysqldump -u dbuser -pdbassword database_name | gzip ' > database_dump.sql.gz
to restore on your local machine
gzip -dc database_dump.sql.gz | mysql -uusername -ppassword

how to migrate a large database to new server

I need to migrate my database from my old server to my new server. I have a very big problem by transferring the database because I have a large database with 5gb. I tried to transfer using c panel transfer but I can't it is not useful. I need a more efficient way to transfer the data.
Can anyone guide me with the full transfer details? How to transfer using import and export or do I need to use any other method?
MySQL type is MyISAM and size is 5gb.
You can try command line if you have access to SSH for both server as command below if not you can try using Navicat application to sync databases
SSH commands
Take mysqldump of database
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
create tar ball of SQL dump file using
tar -zcvf db.tar.gz db.sql
now upload tar.gz file to other server using scp command
scp -Cp db.gz {username}#{server}:{path}
now login on other server using SSH
Untar file using linux cmd
tar -zxvf db.tar.gz
import to database
mysql -u{username} -p {database} < db.sql
Please have a look at syntax though syntax will work but consider this as direction only
Thanks..
For large databases I would suggest to use mysqldump if you have SSH access to the server.
From the manual:
Use mysqldump --help to see what options are available.
The easiest (although not the fastest) way to move a database between two machines is to run the following commands on the machine on which the database is located:
shell> mysqladmin -h 'other_hostname' create db_name
shell> mysqldump db_name | mysql -h 'other_hostname' db_name
If you want to copy a database from a remote machine over a slow network, you can use these commands:
shell> mysqladmin create db_name
shell> mysqldump -h 'other_hostname' --compress db_name | mysql db_name
You can also store the dump in a file, transfer the file to the target machine, and then load the file into the database there. For example, you can dump a database to a compressed file on the source machine like this:
shell> mysqldump --quick db_name | gzip > db_name.gz
Transfer the file containing the database contents to the target machine and run these commands there:
shell> mysqladmin create db_name
shell> gunzip < db_name.gz | mysql db_name
You can also use mysqldump and mysqlimport to transfer the database. For large tables, this is much faster than simply using mysqldump. In the following commands, DUMPDIR represents the full path name of the directory you use to store the output from mysqldump.
First, create the directory for the output files and dump the database:
shell> mkdir DUMPDIR
shell> mysqldump --tab=DUMPDIR db_name
Then transfer the files in the DUMPDIR directory to some corresponding directory on the target machine and load the files into MySQL there:
shell> mysqladmin create db_name # create database
shell> cat DUMPDIR/*.sql | mysql db_name # create tables in database
shell> mysqlimport db_name DUMPDIR/*.txt # load data into tables
Do not forget to copy the mysql database because that is where the grant tables are stored. You might have to run commands as the MySQL root user on the new machine until you have the mysql database in place.
After you import the mysql database on the new machine, execute mysqladmin flush-privileges so that the server reloads the grant table information.

Transfering mysql data from wamp enviroment to centOS

I have copy of .sql file that contains large data. I saved it from phpmyadmin while I was using WAMP for development. Now I am working with CentOS, and I have transferred the data to my VirtualBox running CentOs already.
So, the problem is not about transferring the file but running the .sql file using shell, so the data can be transferred to the new mysql server.
Does anyone know any commands?
Initially I thought moving this entire directory:
C:\wamp\bin\mysql\mysql5.5.24\data
To my new server environment would be a good idea, but I can't seem to find where the data folder is kepyt in centos-mysql.
whereis mysql gives mysql: /usr/bin/mysql /usr/lib/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
I have checked all this folders to find the data folder but to no avail.
if you created the .sql file with mysqldump or if it is otherwise a legal mysql script containing sql commands you can simply pipe this contents to your centOs mysql instance:
$ mysql -uroot -p dbname < dump.sql
where dbname is the name of your database and dump.sql your .sql file.
Follow these steps:
1- In your Windows Environment, from command line(CMD), go to the folder:
cd "C:\wamp\bin\mysql\mysql5.5.24\bin
2- Run: mysqldump -uroot -pYourPassword DataBaseName > myBackup.sql
3- On your centOs machine, open a terminal:
mysql -uroot -p
4- In mysql console:
create database DataBaseName;
exit;
5- Transfer the myBackup from your Windows System, to centOS, open a terminal in
the same directory where myBackup.sql lives:
mysql -uroot -p DataBaseName < myBackup.sql

mysqldump wampee fails

I'm using the portable version of WAMP which is "Wampee" 2.1-beta-2 on Windows XP.
Im launching C:\Wampee-2.1-beta-2\bin\mysql\mysql5.5.8\bin\mysql.exe in order to try a mysqldump.
I do:
mysqldump -u root -p database_name > backup.sql
And it answsers "You have an error in your SQL syntax [..]"
Even this command give me the same result:
mysqldump --help;
How to know if mysqldump is installed and/or enabled? Or my syntax is really wrong?
Thx
Regards
I just found! Mysqldump is another program so you can't access to it from mysql.exe
Open a terminal window such as cmd.exe. Change directory to mysqldump.exe:
cd /go/to/the/mysqldump/folder
Launch mysqldump
mysqldump.exe
Type your commands.
HTH! (:

Dump all databases and reimport on another server?

It's quite simple really, I just can't figure out the syntax.
I want to replicate my server setup on another server.
I can dump all my databases with
mysqldump -uroot -p --alldatabases > all.sql
But how do I import ALL of those into a brand new mysql setup on another server?
mysql -u root -p < all.sql
will do
From command line:
mysql -uroot < all.sql
ps. If you want to see what statement is executed right now you should -v.
if the database servers are both near enough the same version, you could simply rsync the /var/lib/mysql directory over.
rsync /var/lib/mysql root#destination.server.com:/var/lib/ -a
Edit: Please note that there are some issues with this approach which require some additional steps:
http://verens.com/2016/05/11/quick-method-to-clone-a-mysql-database/
mysql -u root -p
that command will open the mysql interactive console, where you'd do this:
source all.sql
execute the dump file from shell (dump file should contain the CREATE DATABASE syntax)
mysql -uroot < /path/to/file.sql
or execute from mysql
source /path/to/file.sql