Swapping/toggling two MySQL databases - mysql

We recently lost some data from our live DB. We restored an older backup as our current live DB, and have been repairing a more recent partially broken DB as a test DB. Both are running on the same server and the plan is the testing DB will replace the live DB as soon as we're happy the fixes are valid.
Is there an easy way to swap the two DBs, e.g. swap their names, so we can just switch which one is used by the website for some last-minute checks? i.e. we want to set the repaired DB as the live one, but be able to flip back without re-building/re-importing a whole exported version.
I don't know if MySQL allows renaming DBs in this way.

To swap two database schemas that are named db1 and db2:
mysqladmin -uroot -pmypassword create swaptemp
mysqldump -uroot -pmypassword --routines db1 | mysql -u root -pmypassword swaptemp
mysqladmin -uroot -pmypassword drop db1
mysqladmin -uroot -pmypassword create db1
mysqldump -uroot -pmypassword --routines db2 | mysql -u root -pmypassword db1
mysqladmin -uroot -pmypassword drop db2
mysqladmin -uroot -pmypassword create db2
mysqldump -uroot -pmypassword --routines swaptemp | mysql -u root -pmypassword db2
mysqladmin -uroot -pmypassword drop swaptemp
Steps:
Copy the lines into Notepad.
Replace all references to "db1", "db2", "mypassword" (+ optionally "root") with your equivalents.
Execute one by one on the command line (entering "y" when prompted).

Related

Backup all privileges related to specific user of MySQL

I want to backup all privileges related to specific user (for example u_1) from a MySQL database and restore that in another server. As described here, The 'mysql' database contains users/privileges/passwords. So I have to backup all related data from all tables of mysql database (mysql.user,mysql.db, ...). I can run this command:
$ mysqldump -u root --flush-privileges -r mysql.sql -p mysql
But of course the mysql.sql contains all users and all privileges.
Also I tried this command:
$ mysqldump -u root -p --where="user.user='u_1'" mysql user> mysql.sql
But as expected, it only contains a row of mysql.user table.
Is there a way to strip out other users except of u_1?
Try these options (line breaks for clarity):
$ mysqldump -u root -p
--where="user='u_1'"
--complete-insert
--extended-insert
--no-create-info
mysql
user db tables_priv columns_priv procs_priv proxies_priv
> mysql.sql
Or... let's call the above solution "the hard way."
This should be the easy way:
$ mysql -u root -p
--skip-column-names
-e "SHOW GRANTS FOR 'u_1';"
> grants.sql
I would use:
pt-show-grants --only u_1
pt-show-grants is a tool in the free Percona Toolkit.
See https://www.percona.com/doc/percona-toolkit/LATEST/pt-show-grants.html

Ignoring table in mysqldump?

Hi I'm doing a database copy and paste from a master to a slave. However there is a table on the slave that I don't want to be overwritten by the master.
I have been trying out the following bash script which is ran via cron job - but it keeps overwriting the slave table I want to ignore. What am I doing wrong?
#!/bin/bash
#Database login credentials - need to be changed accordingly
dbHost="localhost"
#Master (Staging)
dbMastUser="admin_site"
dbMastName="admin_site_pineapple_master"
#Slave (Live)
dbSlavUser="admin_sync"
dbSlavName="admin_site_pineapple_slave"
dbPass="ExamplePassword"
EXCLUDED_TABLES=(
forms-responses
)
IGNORED_TABLES=''
for TABLE in "${EXCLUDED_TABLES[#]}"
do :
IGNORED_TABLES+=" --ignore-table=${dbMastName}.${TABLE}"
done
#Update the database from the Master to the Slave
mysqldump -h ${dbHost} -u ${dbMastUser} -p${dbPass} ${dbMastName} ${IGNORED_TABLES} | mysql -h ${dbHost} -u ${dbSlavUser} -p${dbPass} ${dbSlavName}
For ignoring tables you must use this syntax
mysqldump -h {hostname} -u {username} -p{password} --ignore-table test.votes test > E:/db_backups/test_1480080906.sql
general syntax
mysqldump -h {hostname} -u {username} -p{password} --ignore-table dbname.tbl_name db_name > E:/db_backups/test_1480080906.sql
Note : we need to set the options to ignore some tables
Thanks
Suman W.
/usr/local/mysql/bin/mysqldump -uroot -p{pwd} --skip-lock-tables --databases f6dms $(mysql -uroot -p{pwd} -Df6dms -Bse "show tables like 'tm_monitor_avg_price_%'"|awk '{print "--ignore-table=f6dms."$1}'|xargs)| gzip > /data/backup/database_f6dms_`date '+%m-%d-%Y'`.sql.gz;

creating mysqldump to backup database

I know how mysqldump works.
But dont know where to use it?
If I execute this command after starting mysql program then it says error.
I am using ubuntu. So how can I use this utility?
Backup your database this way too..
mysql -u root -p DB_NAME > db_name_backup.sql
If you want to backup all database simply run this
mysql -u root -p > mysql_db_backup.sql
You will learn more about mysql and mysqldump here..
Guide:
mysqldump and mysql
MySQL Database Backup using mysqldump
shell> mysqldump --opt db_name > backup-file.sql
You can read the dump file back into the server like this:
shell> mysql db_name < backup-file.sql
Or like this:
shell> mysql -e "source /path-to-backup/backup-file.sql" db_name
mysqldump is also very useful for populating databases by copying data
from one MySQL server to another:
shell> mysqldump --opt db_name | mysql --host=remote_host -C db_name
It is possible to dump several databases with one command:
shell> mysqldump --databases db_name1 [db_name2 ...] > my_databases.sql
If you want to dump all databases, use the --all-databases option:
shell> mysqldump --all-databases > all_databases.sql
If tables are stored in the InnoDB storage engine, mysqldump provides a
way of making an online backup of these (see command below). This
backup just needs to acquire a global read lock on all tables (using
FLUSH TABLES WITH READ LOCK) at the beginning of the dump. As soon as
this lock has been acquired, the binary log coordinates are read and
lock is released. So if and only if one long updating statement is
running when the FLUSH... is issued, the MySQL server may get stalled
until that long statement finishes, and then the dump becomes
lock-free. So if the MySQL server receives only short (in the sense of
"short execution time") updating statements, even if there are plenty
of them, the initial lock period should not be noticeable.
shell> mysqldump --all-databases --single-transaction > all_databases.sql
For point-in-time recovery (also known as “roll-forward”, when you need
to restore an old backup and replay the changes which happened since
that backup), it is often useful to rotate the binary log (see
Section 8.4, “The Binary Log”) or at least know the binary log
coordinates to which the dump corresponds:
shell> mysqldump --all-databases --master-data=2 > all_databases.sql
or
shell> mysqldump --all-databases --flush-logs --master-data=2 > all_databases.sql
The simultaneous use of --master-data and --single-transaction works as
of MySQL 4.1.8. It provides a convenient way to make an online backup
suitable for point-in-time recovery if tables are stored in the InnoDB
storage engine.
For more information on making backups, see Section 6.1, “Database
Backups”.
mysqldump -u MYSQL_USER -h MYSQL_SERVER -pMYSQL_PASS --all-databases > "dbs.sql"
You use it directly on the terminal, just like mysql it self, and pass the parameters directly to it.
mysqldump -u [user] -p[password] [database name] > dumpfilename.sql
yes you can.
see http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html for more information on the tool.
If it's an entire DB, then:
$ mysqldump -u [uname] -p[pass] db_name > db_backup.sql
If it's all DBs, then:
$ mysqldump -u [uname] -p[pass] --all-databases > all_db_backup.sql
If it's specific tables within a DB, then:
$ mysqldump -u [uname] -p[pass] db_name table1 table2 >
table_backup.sql
You can even go as far as auto-compressing the output using gzip (if your DB is very big):
$ mysqldump -u [uname] -p[pass] db_name | gzip > db_backup.sql.gz
If you want to do this remotely and you have the access to the server in question, then the following would work (presuming the MySQL server is on port 3306):
$ mysqldump -P 3306 -h [ip_address] -u [uname] -p[pass] db_name >
db_backup.sql
To IMPORT:
ype the following command to import sql data file:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
In this example, import 'data.sql' file into 'blog' database using vivek as username:
$ mysql -u sat -p -h localhost blog < data.sql
If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
OR use hostname such as mysql.cyberciti.biz
$ mysql -u username -p -h mysql.cyberciti.biz database-name < data.sql
If you do not know the database name or database name is included in sql dump you can try out something as follows:
$ mysql -u username -p -h 202.54.1.10 < data.sql
REfer: http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html

how can i take backup of mysql database tables

i want to move mysql database tables which resides in one computer to another computer. how can i create dump file as we created in Oracle ?
i m using exp command but not working.
Use mysqldump.
mysqldump -u <username> -p<password> <db_name> > <filename>.sql
To import, create empty database named <db_name>, thena -
mysql -u <username> -p<password> <db_name> < <filename>.sql
To export all databases -
mysqldump -u <username> -p<password> --all-databases > <filename>.sql
mysqldump -u <username> -p<password> -h <hostname> <dbname> <tablename> > filename.sql
Now you may need to take the dump of just the schema. For example you use a command called like.
mysql> show create table tablename;
this will give you the query that created the table. Now for some reason you need to take schema dump of all the tables inside you database/databases you may use -d option like this
mysqldump -d -u <username> -p<password> -h <hostname> <dbname> > filename.sql
The -d option means "without data".
Now you have full dump without the data and just the schema.

mysql dump, windows, seperate files for each DB, all databases option

Does anybody have an example on how to dump all databases uses mysql dump? And possible all a new file for each DB?
I'm using the follow command:
mysqldump -u root -p pw --all-databases > backup.sql;
It's returning with "You have an error in your SQL sytax";
Thanks!
There is an error in your command, it should be no space after -p,
like
mysqldump -u root -ppw --all-databases > backup.sql;
I not sure how many database you have, usually you can do this :-
mysqldump -u root -ppw db_a > db_a.sql;
mysqldump -u root -ppw db_b > db_b.sql;
...
... for all the databases