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'
Related
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%';
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
Normally I have database already created. So this command line works find:
mysql -h -u -p [databaseName] < dump.sql
I have an import.sql file that has a top query that creates the database, if it doesn't already exist.
Is there a way to import the sql file, but without needing to select a pre-existing database?
Invoke the commandline without specifying the database
mysql -h -u -p < dump.sql
inside dump.sql, after you create the database add
USE databasename;
While creating the dump file using mysqldump you can use the switch --add-drop-database. This will include a statement to drop the database first. So in the subsequent statement, a fresh database will be created since no database with the given name exists
See mysql documentation for more
Is there a SQL command that 'resets' the database in MySQL?
By reset I mean, all rows are deleted and auto increment are reset.
You're looking for TRUNCATE, as in TRUNCATE TABLE mystuff
More info: http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
mysqldump -uuser -hhost -p --no-data name_of_database > backup_file_name.sql
The above command will keep the entire structure of your database but without the data. (that's what the --no-data param does).
Now when you want to reset your database you just:
mysql -uuser -hhost -p < backup_file_name.sql
View the .sql file created to see what its doing. There are a bunch of options you can add to the mysqldump command.
Enjoy.
mysql> -u username -p [database] < file.sql
restores database. I do not have a create clause in my dumped file, hence I need to create a database and then restore. I can do this to create.
mysql> create database [database];
Fair enough. Now how can I achieve the above restoration in one line? Is there anyway to restore a database by creating the database before that with a single line of code?
I can certainly live without it, but would be nice to know.. Thanks..
(echo "create database XYZ; use XYZ;"; cat file.sql) | mysql -u username -p
could do the trick.