Skip database on import of MySQL dumpfile - mysql

I created a dumpfile with this command:
mysqldump -p3307 --quick -u root --password="password" --all-databases > all_databases04292011.sql
I then attempted to import this dumpfile into a new MySQL server, however it fails trying to write to the information_schema database. Is there a way to skip this database on import?

You can write a program editing the all_databases04292011.sql dumpfile, to delete all the records in information_schema database.
It is not that hard, since the dumpfile is well structured.
Just open all_databases04292011.sql using your favourite editor, then find
--
-- Current Database: `information_schema`
--
Delete every line below this until you see the next
--
-- Current Database: `xxxxxx`
--

Related

Error restore dump MySQL8 - ERROR 3552 (HY000) at line 19044: Access to system schema 'mysql' is rejected

We have a cron that does a full dump of the MySQL5 server, and in tests of restore on a empty instance, it restores all bases, including mysql with mysql.user carrying users and permissions together.
In MySQL8 because mysql base is system, the --add-drop-database and --all-databases attributes conflict giving an error in the restore "ERROR 3552 (HY000) at line 19044: Access to system schema 'mysql' is rejected.", as it is not allowed to drop the mysql base.
Has anyone managed to get around this situation and bring users and privileges together in MySQL8 in same dumpfile?
This is the command i run to dump:
mysqldump --add-drop-database --flush-logs --single-transaction --ignore-table=mysql.innodb_index_stats --ignore-table=mysql.innodb_table_stats --quick --all-databases --triggers --routines --events -u root --password='senha' -P 3306 -h 1.1.1.1 | bzip2 > /tmp/backup.sql.bz2
The problematic SQL:
/*!40000 DROP DATABASE IF EXISTS `mysql`*/;
The best way to walk around this, just open the dump SQL file, delete this SQL,
if the file is too big, use sed.
I ran into this same scenario. I dumped a broken instance with all databases and using add-drop-statement to try and save the data, but when I went to restore it I was blocked. You can no longer drop the mysql system database.
My database backup was something like 150gb, and opening it manually was not an option (a shame as i could tell by doing head -n 50 backup.sql that the problematic statement was within the first few lines).
the statement to remove was
/*!40000 DROP DATABASE IF EXISTS `mysql`*/;
and the sed command for me was:
sed -i 's/\/\*!40000 DROP DATABASE IF EXISTS `mysql`\*\/;/ /g' backup.sql
I would paste the statement into an empty text file first, and run the command to confirm that it actually works. This way you don't waste a ton of time on the execution of a very large backup file -- as there's a chance with your version of sed, or OS, that it might resolve the regular expression differently.

importing mysql .sql file appears to randomly selects which tables to import or not

I have database A. I issue this command against it:
mysqldump --host=localhost -uroot -p"mypassword" my_db_name > file.sql
now I take this file to machine B, running mysql too. I create a database:
create database newdb;
I then:
mysql --host=localhost -uroot -proot newdb < file.sql
My problem is that not all tables that exist in file.sql are created in the new database! I clearly see CREATE TABLES users in the content of the file.sql followed by thousands of INSERT calls for content in that table.
But users table is never created in the new database. I am completely lost as to why.
If you have foreign keys, the tables might be created in the wrong order and since the constraints can't be created, creating the table fails. Try adding SET FOREIGN_KEY_CHECKS=0 in the beginning of the dump and SET FOREIGN_KEY_CHECKS=0 at the end.
Delete whole newdb database;
Restart mysqld;
Run mysqlcheck --repair --all-databases -u root -p root on machine B;
Create newdb again (or maybe call it newdb2 just to be sure);
Delete file.sql on machine B, copy file.sql again from machine A and import by mysql --host=localhost -uroot -proot newdb < file.sql;
Run SHOW engine innodb STATUS; and or show table status and analyze results.
Copy a CREATE TABLE that failed to work. In the commandline tool "mysql", paste that. What messages, if any do you get? Does it create the table?
Please provide that CREATE for us; there may be some odd clues.
Also provide SHOW VARIABLES LIKE '%enforce%';

Importing a MySQL Database on Localhost

So I wanted to format my system and I had a lot of works that I have done on my localhost that involves databases. I followed the normal way of backing up the database by exporting it into an SQL file but I think I made a mess by making a mistake of backing up everything in one SQL file (I mean the whole localhost was exported to just one SQL file).
The problem now is: when I try to import the backed up file I mean the (localhost.sql), I get an error like
tables already exist.
information_schema
performance_schema
an every other tables that comes with Xampp, which has been preventing me from importing the database.
These tables are the phpmyadmin tables that came with Xampp. I have been trying to get past this for days.
My question now is that can I extract different databases from the same compiled SQL database file?
To import a database you can do following things:
mysql -u username -p database_name < /path/to/database.sql
From within mysql:
mysql> use database_name;
mysql> source database.sql;
The error is quite self-explanatory. The tables information_schema and performance_schema are already in the MySQL server instance that you are trying to import to.
Both of these databases are default in MySQL, so it is strange that you would be trying to import these into another MySQL installation. The basic syntax to create a .sql file to import from the command line is:
$ mysqldump -u [username] -p [database name] > sqlfile.sql
Or for multiple databases:
$ mysqldump --databases db1 db2 db3 > sqlfile.sql
Then to import them into another MySQL installation:
$ mysql -u [username] -p [database name] < sqlfile.sql
If the database already exists in MySQL then you need to do:
$ mysqlimport -u [username] -p [database name] sqlfile.sql
This seems to be the command you want to use, however I have never replaced the information_schema or performance_schema databases, so I'm unsure if this will cripple your MySQL installation or not.
So an example would be:
$ mysqldump -uDonglecow -p myDatabase > myDatabase.sql
$ mysql -uDonglecow -p myDatabase < myDatabase.sql
Remember not to provide a password on the command line, as this will be visible in plain text in the command history.
The point the previous responders seem to be missing is that the dump file localhost.sql when fed into mysql using
% mysql -u [username] -p [databasename] < localhost.sql
generates multiple databases so specifying a single databasename on the command line is illogical.
I had this problem and my solution was to not specify [databasename] on the command line and instead run:
% mysql -u [username] -p < localhost.sql
which works.
Actually it doesn't work right away because of previous attempts
which did create some structure inside mysql, and those bits in localhost.sql
make mysql complain because they already exist from the first time around, so
now they can't be created on the second time around.
The solution to THAT is to manually edit localhost.sql with modifications like
INSERT IGNORE for INSERT (so it doesn't re-insert the same stuff, nor complain),
CREATE DATABASE IF NOT EXISTS for CREATE DATABASE,
CREATE TABLE IF NOT EXISTS for CREATE TABLE,
and to delete ALTER TABLE commands entirely if they generate errors because by then
they've already been executed ((and INSERTs and CREATEs perhaps too for the same reasons). You can check the tables with DESCRIBE TABLE and SELECT commands to make sure that the ALTERations, etc. have taken hold, for confidence.
My own localhost.sql file was 300M which my favorite editor emacs complained about, so I had to pull out bits using
% head -n 20000 localhost.sql | tail -n 10000 > 2nd_10k_lines.sql
and go through it 10k lines at a time. It wasn't too hard because drupal was responsible for an enormous amount, the vast majority, of junk in there, and I didn't want to keep any of that, so I could carve away enormous chunks easily.
unzip -p /pathoffile/database_file.zip | mysql -uusername -p databsename;
Best way to import database in localhost has simple 5 steps:
zip sql file first to compress databse size.
go to termianl.
create empty database.
Run Command unzip databse With Import database: unzip -p /pathoffile/database_file.zip | mysql -uusername -p databsename;
Enter Password

How to Import MySQL dump file with Red Hat 6

I have a VM that I'll be installing mysql server on. I have a dump file that I need to import into mysql. The first line says this:
-- MySQL dump 10.13 Distrib 5.6.20, for linux-glibc2.5 (x86_64)
The file already has databases and tables, along with the structure and data.
grep -i 'current database' db_dump.txt
-- Current Database: `db1`
-- Current Database: `db2`
-- Current Database: `db3`
grep -i 'data for table' db_dump.txt
-- Dumping data for table `TABLE1`
-- Dumping data for table `TABLE2`
-- Dumping data for table `TABLE3`
As you can see, its a .txt file and this is partly where my confusion is coming in. Much of what I have read is that in order to import a text file you must already have the database's and tables created. However they are already defined in the file. So that lead me to running a command such as this:
mysql -u <user> -p < filename.dump
But some of the documentation says you must have a .sql file in order to do this. So can I just rename my .txt file to .sql or just import as is? What would the command look like? I am really a noob when it comes to MySQL so any guidance is much appreciated.
You command is correct. The extension of the filename is not important. It is what's inside that matters.
Oh! There is actually one thing that needs to change:
mysql -u<username> -p database_name < filename.dump
The database name should be the last part of the command and the username needs to go after -u, so if you are the root user you should type:
mysql -uroot -p database_name < filename.dump

How to import a MySQL dump from command line WITH overwrite

i googled a lot and i can't found nothing about it !
[root#someday backups]# mysql -u username_1 -p db_1 < tables_to_import/tables.sql
ERROR 1050 (42S01) at line 19: Table 'ps_customer' already exists
with mysql -f is the same. i wish simply import that .sql and rewrite that tables, can someone help me ?
p.s. i know that when you export a db you can choose option "DROP TABLE" but if i have a backup, without this declaration ? how can i force ? Thanks
When you do mysqldump add --add-drop-table (like twihoX mentioned). Then do your import as usual. So something like:
mysqldump --add-drop-table -u user -p db_1 > dumpfile.sql
mysql -u user -p db_1 < dumpfile.sql
Are you trying to overwrite the entirety of the database? If so, you could manually drop all the tables, and then run your import script. This is easy to do in phpmyadmin. If you're using the CLI, the fastest way would be to use DROP DATABASE databasename and then create database, though I think you'd then have to re-grant privileges for any non-root users.
Another option would be to open up your dump file and add DROP TABLE tablename before each of the CREATE TABLE commands. You could probably do this easily with some clever regex.
I'd suggest --add-drop-table option.
I know this question is a bit old and it's been marked as answered correctly, I'd just like to add this here for those (like me) who didn't use --add-drop-table when exporting.
What you can do is log in to MySQL and drop the tables that you plan to overwrite, then use --force on import.
So login to MySQL
mysql -h HOSTNAME - USERNAME -p
then tell mysql which database you wish to use
mysql> use DATABASE_NAME
drop tables that you want to overwrite
mysql> DROP TABLE my_images;
Then you are ready to import, so log out of mysql and back to where your SQL file was uploaded and run the following command
$ mysql --force -uDB_USER -p DB_NAME < myuploadedfile.sql
This will force MySQL to continue importing any new tables and ignore the 'table already exists error'