Restore database from dump using mysql script - mysql

I'm testing a python script that requires me to manually clear and restore a fresh copy of my database for the purposes of debugging it. Basically each time the script cracks up, it breaks my database and I need a fresh copy. It would be really convenient if I could just run a sql script to so so. This is trivial except for the part when I re-import the data from a file on my machine; I can't seem to figure out how to do that within the sql script.
I can do it by clicking through the GUI as detailed here.
And I can do it by running this in the terminal
mysql -u root -p testdb < filename.sql
But what I would really like to do is to throw a line in my sql script along with my drop and create commands. Something like this
drop schema testing2;
create database testing2;
use testing2;
some command to restore from ....\Dump20230125.sql
I read here that I could use source ....\Dump20230125.sql but mysql workbench throws a syntax error and says that "source is not valid at this position"
Is there a way to accomplish what I'm after?

Related

Skip triggers while restoring MySQL dump using Linux command

I have a large dump(~50GB) of MySQL database(Multiple databases backup). I am restoring it to a new Server which is taking a long time. I think it is taking this much time because it is huge data. the command ( gunzip < 1922-1648-329-75_all-databases.sql.20220305-0000.gz | MySQL -u test -p) also working fine and it started importing. But after some time, I'm getting an error called "Unknown column 'id' in 'OLD'". I have troubleshot and found that this error is coming from one of the triggers in the backup file.
I don't really need triggers on the new server. Is there a command-line option to use in MySQL which will allow me to skip the triggers while restoring the dump?
Below is the Restore Commane which I am using.
gunzip < 1922-1648-329-75_all-databases.sql.20220305-0000.gz | MySQL -u test -p
Recreate the dump if you can, using the --skip-triggers option. You will have to recreat them afterwards.
https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_triggers
There is no option available to disable the triggers on import.
rewrite your faulty trigger code.
you have an INSERT trigger with OLD in it, which is not supported.
Maybe it is acopy paste error, but such things must be made before making a backup runs.
the link below shows you how to select all triggers, this should help to find the problematic trigger
How do you list all triggers in a MySQL database?

MySQL Workbench - How to clone a database on the same server with different name?

I am using MYSQL Workbench and I want to clone a database on the same server with different name. It should duplicate the all the tables structure and data into the new database.
I know the usual way is probably using data export to generate a sql script of the database and then run the script on the new database but I encounter some issues with it.
Anyway, is there any better way or easier way to do so?
You can use migration wizard from MySQL Workbench. Just choose the same local connection in both source and target selection, then change schema name on manual editing step. If nothing appears on manual editing step click next and the source and targets will appear. Click slowly on the source database name and edit to the correct name. Go thorough to the end and voilĂ  - you have two identical databases with different names. Note you must have created the target database already and granted permissions to it for the MySQL Workbench user.
I tried to do it in MySQL Workbench 8.0. However I kept receiving an error regarding column-statics. The main idea is to use mysqldump.exe, located in the installation directory of MySQL Workbench, to export the data. So, supposing a Windows oriented platform:
Open Powershell, navigate to mysqldump.exe directory. In my case the command is:
cd C:\Program Files\MySQL\MySQL Workbench 8.0 CE
Export database by executing mysqldump providing the right arguments:
./mysqldump.exe --host=[hostServerIP] --protocol=tcp --user=[nameOfUser] --password=[yourPassword] --dump-date=FALSE --disable-keys=FALSE --port=[portOfMysqlServer] --default-character-set=utf8 --skip-triggers --column-statistics=0 "[databaseName]"
Without changing directory, import the exported file (.sql) by using the following command in Powershell:
Get-Content "[pathToExportedDataFile]" | ./mysql.exe --user=[nameOfUser] --password=[yourPassword] --port=[portOfMysqlServer] --host=[hostServerIP] --database=[nameOfNewDatabase] --binary-mode=1
You can check in the documentation here for more information regarding the mysqldump options.
Please note the following:
Do not forget to replace the values in [] with your own values and remove the []. Do not remove the quotes("") where the are present.
Do not switch Powershell for cmd or something like git-bash, since the above will not work.
As far as step 3 is concerned, I created the new database from MySQL Workbench and then ran the powershell command.
List item First, create a new database using CREATE DATABASE statement.
Second, export all the database objects and data of the database from which you want to copy using mysqldump tool.
Third, import the SQL dump file into the new database.

What parameters does MySQL Workbench pass to mysqldump?

I need to write a script to automate MySQL backup of a database. So to determine what I will need, I go into MySQL Workbench, select the Schema, select Data Export, set a couple of controls (at the moment: Export to Self-Contained File & Include Create Schema) and Start Export.
Export Progress shows me command-line:
Running: mysqldump --defaults-file="/tmp/tmpTbhnzh/extraparams.cnf" --user=*** --host=*** --protocol=tcp --port=3306 --default-character-set=utf8 --skip-triggers "<schema-name>"
I need to know what is in that temporary "defaults file" if I'm to replicate whatever it is that MySQL Workbench passes to mysqldump. But the backup completes so quickly and deletes the file that I can't even copy it, of course!
Is there a way I can know just what arguments Workbench is passing to mysqldump so I can know I'm generating a good, robust script? (To be clear: I'm sure I can look up the mysqldump documentation to find arguments corresponding to whatever UI items I fill in explicitly, but I'm wondering what other "goodies" MySQL Workbench might know about and put in the parameters file.)
A bit of digging about in the python scripts (there's one called wb_admin_export.py) and the answer is....not very exciting... it's your password.
It also includes ignore-tables if there are any to ignore.

Importing Data and Schema to MySQL Workbench

I'm trying to learn SQL and I downloaded a database to practice. I downloaded two files of extension .sql, one is the schema and the other one the actual data. I've also installed MySQL Workbench. I've been googling and I've been trying things to solve this but I don't understand Workbench and I can't load the database.
Where do I import the schema and the data in order to try queries ?
Any help would be really appreciated.
This is simple in Workbench, and I'll use the freely available sakila database as an example. Feel free to apply this to your situation:
Download "sakila" from here: http://dev.mysql.com/doc/index-other.html
Extract it somewhere, in my case, onto the Desktop into a new sakila-db/ directory
Open Workbench
In the Schema Navigator, right-click an empty area and choose "Create Schema"
In the schema creation wizard, name it "sakila", keep the defaults, hit "Apply", finish wizard
Go to "File" -> "Run SQL Script..."
Choose "sakila-schema.sql", make sure "Default Schema Name" is blank or select "sakila", execute
Go to "File" -> "Run SQL Script..."
Choose "sakila-data.sql", execute
Click the "refresh" icon in the Workbench Schema Navigator (or restart Workbench)
Now, use the populated sakila database :)
Steps (4) and (5) are optional in this case (as executing sakila-schema.sql creates the schema), but the idea is worth mentioning.
Here's how it would look when loading th script into the SQL IDE:
The accepted answer is from 4 years ago, so I thought I'd give an update as in MySQL Workbench 6.3 the procedure is a bit different.
You have to select the menu item Server -> Data Import -> Import from Self-Contained File and select the SQL file containing the database you want to import.
In Default Target Schema, select the database you want to import the SQL dump to, or create a new empty database via New...
Then click on Start Import.
You could use mysql console from terminal. Login through the user id and pass. Then create a Database from the following command is the .sql file does not have one such command to create so.
Create database db-name
use db-name;
SOURCE xyz.sql;
Source command would load the the content from xyz.sql to your database created. This would be reflected later in workbench indeed.
Its very easy on Linux platform just follow below mentioned steps, After downloading zip file of sakila-db, extract it. Now you will have two files, one is sakila-schema.sql and other one is sakila-data.sql.
Open terminal
Enter command mysql -u root -p < sakila-schema.sql
Enter command mysql -u root -p < sakila-data.sql
Now enter command mysql -u root -p and enter your password, now you have entered into mysql system with default database.
To use sakila database, use this command use sakila;
To see tables in sakila-db, use show tables command
Please take care that extracted files are present in home directory else provide the absolute path of these files in all above commands.

mysql import trouble using XAMP

originally i was using easyPHP (windows) then i switch to mac and used MAMP.
i archive my db every once in a while and right before i reformatted. The export was made by going into the root of phpMyAdmin and using the export function. Now i am trying to import the data i get this error "#1044 - Access denied for user 'root'#'localhost' to database 'information_schema'". Doing other things i got errors like
"#1146 - Table 'test_db.COLLATION_CHARACTER_SET_APPLICABILITY' doesn't exist", "#1146 - Table 'test_db.CHARACTER_SETS' doesn't exist " and "#1146 - Table 'test_db.COLUMNS' doesn't exist" and "#1046 - No database selected"
How do i get MAMP to import and ignore any access errors and continue so my DB is filled to the previous state? I rather not write an app to do this but if i had to what libs would i use to parse the sql statements in the sql dump? It doesnt look hard to parse. It looks like a semicolon separates the statements. But what about escape and unescape issues? how do i handle that?
The first error indicates that something is very wrong with your setup. information_schema is an internal DB which keeps data about other parts of the system (meta-data). You should try reinstalling your MySQL server (or even MAMP as a whole).
Second of all: the dump files can be imported using the mysql command line client like this:
mysql -p -u root test_db < dump.sql
One thing to remember is that "test_db" needs to be created before the dump is restored. An other possible problem might be that the dump/restore is being done by different versions of MySQL (ie. 5.0 vs 5.1). For this you could try the --force command in the mysql command line to skip over failed executions, however be aware that this might not correctly restore your data.