Problem creating MySQL database from dump file - mysql

I am attempting to follow the instructions found here: create database from dump file in MySQL 5.0 to create a mySQL database from a dump file.
I created the dump file from an access database using the MS Access to MySQL tool (described here: How can I convert an MDB (Access) file to MySQL (or plain SQL file)?).
However, when I follow the instructions in the first link, the resulting database has no tables. To be more specific, if I run the command SHOW TABLES IN test_db;, the output is Empty set (0.01 sec).
I'm stumped on what my next troubleshooting step should be. Could there have been an issue when creating the dump file? If that occurred, how would I check it?
In case it matters, the MySQL server that I am attempting this on is running in a linux environment.
-edit in response to comments-
The dump file is created by the linked GUI wizard tool, but after that point, I run the three SQL lines from the first linked question:
create database test;
use test;
source /path_to_dump_file/dump_file.sql;
the output is a whole bunch of
Query OK, 1 row affect (0.00 sec)
That's the extent of it.
-edit2- Ok, I think I misunderstood one of the questions: here is the first several lines of the dump file sql code:
CREATE DATABASE IF NOT EXISTS `movedb`;
USE `movedb`;
#
# Table structure for table 'County by Station'
#
DROP TABLE IF EXISTS `County by Station`;
CREATE TABLE `County by Station` (
`Polygon/Station` VARCHAR(255),
`Tributary or Water Body` VARCHAR(255),
`Latitude` DOUBLE NULL,
`Longitude` DOUBLE NULL,
`County` VARCHAR(255),
INDEX (`Polygon/Station`)
) ENGINE=myisam DEFAULT CHARSET=utf8;
SET autocommit=1;
#
# Dumping data for table 'County by Station'
#
this is preceded by a description of the export options in the GUI and followed by a bunch of INSERT INTO... code

Related

I have an older .sql file (exported from 5.0.45) I am trying to import into a newer version of MySQL via phpMyAdmin. Receiving errors

Receiving the following error message:
Error
Static analysis:
1 errors were found during analysis.
This option conflicts with "AUTO_INCREMENT". (near "AUTO_INCREMENT" at position 692)
SQL query:
-- phpMyAdmin SQL Dump -- version 2.8.2.4 -- http://www.phpmyadmin.net -- -- Host: localhost:3306 -- Generation Time: Mar 23, 2020 at 03:58 PM -- Server version: 5.0.45 -- PHP Version: 5.2.3 -- -- Database: weir-jones -- -- -------------------------------------------------------- -- -- Table structure for table categories -- CREATE TABLE categories ( number int(11) NOT NULL auto_increment, section varchar(255) NOT NULL, parent_id varchar(10) NOT NULL, title varchar(200) NOT NULL, type varchar(255) NOT NULL, content text NOT NULL, display_order int(11) NOT NULL, PRIMARY KEY (number) ) ENGINE=MyISAM AUTO_INCREMENT=126 DEFAULT CHARSET=utf8 AUTO_INCREMENT=126
MySQL said: Documentation
1046 - No database selected
============================================
I have tried importing with all compatibility modes. No luck.
old database is gone, cannot export again.
Any help would be appreciated.
Brendan
If you ask for the 1046 No database selected then it is what it means. You exported a table from a database without the USE xxx.
So I would suggest try importing this within a database or add the USE clause on top on your SQL file.
Another thing:
If you ask a question on Stackoverflow make sure to read the "formatting rules". Wich means you can organzie your question.
It is actually quite hard to read what error you have. Use emphasis, code blocks and such things like:
CREATE table_blub
col1 CHAR(120) NOT NULL,
col2 INT(5)...
By this someone can better read what is code and what is the error and of course what is the actual question.
Eurobetics is correct, this is because the .sql file doesn't specify what database to work with. That's no problem, you can just create the database on your new server and import the file in to that. Since you're importing through phpMyAdmin, first use the "New" text in the left-hand navigation area to create a new database (you don't need to put any tables in it). Once the database is created, phpMyAdmin puts you in the database structure page. (If you happen to navigate away or are coming back after you've already created the database, just click the existing database in the navigation pane). Look at the tabs along the top row and use the "Import" tab there (or drag and drop your .sql file here).
Being inside that database page tells phpMyAdmin that you want to import to that database specifically, whereas if you're on the main home page, the Import button there isn't attached to any particular database, which leads to your error.
You could technically add the SQL commands to create the database and USE the database in to the .sql file, but in this case that doesn't seem like it's needed and would just be making more work for you.

Can I specify different data directory for each database running on a single MySQL installation?

My question is:
Can I specify different data directory for each database running on a single MySQL installation? I have multiple large databases, I want to point each to it's own directory, each on a different mount (unique disk).
Image is worth a thousand words, so let me illustrate:
Trying something like this for some reason creates DB and Table, but ignores data directory and index directory options:
CREATE DATABASE `DB1` /*!40100 COLLATE 'latin1_swedish_ci' */;
USE DB1;
CREATE TABLE `onDisk1` (
`id` INT(11) NULL DEFAULT NULL
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
DATA DIRECTORY='/mnt/windows/share_data/mysql'
INDEX DIRECTORY='/mnt/windows/share_data/mysql'
;
Mysql support this, You can follow steps here
https://dev.mysql.com/doc/refman/5.6/en/multiple-data-directories.html

How to get query that would recreate sql table in PHPMyAdmin

I have table on MySQL server and would like to get the SQL that would re-create the table.
How do I get the query to recreate the SQL table?
MySQL supports SHOW CREATE TABLE to return the SQL that was used to create a table.
From their docs:
mysql> SHOW CREATE TABLE t;
CREATE TABLE t (
id INT(11) default NULL auto_increment,
s char(60) default NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM
This can be useful if you have just a connection to the DB, versus a CLI on the server. If you have a CLI, use mysqldump like liamgriffiths recommends.
mysqldump can do it - http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
You can use it like this:
mysqldump [databasename] [tablename] > mysqltablesql.sql
If you are using phpMyAdmin, select a table then click the 'Export' tab. One of the output types is SQL. Selecting this will generate a SQL script to create the table and insert all of the data that is currently in the table. Make sure under the dump data option to select 'Structure and Data'
mysqldump [OPTIONS] database [tables]>backup-file.sql
If you don't give any tables or use the --databases or --all-databases, the whole database(s) will be dumped.
You can get a list of the options your version of mysqldump supports by executing mysqldump --help.
Note that if you run mysqldump without --quick or --opt, mysqldump will load the whole result set into memory before dumping the result. This will probably be a problem if you are dumping a big database.
If you have access to phpMyAdmin, you can get this code through the Export tab on the table that you require.
Select SQL then make sure you export "Structure" ("CREATE table" code) and "Data" ("INSERT" code) with Export type set to "INSERT".
I'd go with #liamgriffiths proposal of mysqldump, but you could also try create table <new-table-name> like <old-table-name> followed by insert into <new-table-name> select * from <old-table-name>

Export DB Tables via phpMyAdmin In Non-Alphabetical Order

I have a MySQL database from a Joomla MultiSite installation where it has a set of tables with different prefixes for each Joomla site. When I export the db via phpMyAdmin it creates a SQL file where the tables are created and populated in alphabetical order. The problem is that the tables for the slave sites have dependencies on the tables for the master site, but alphabetically their prefixes are ahead of the master site. So the export works fine but when I try importing I get error after error and have to manually move sections around in the SQL file to make sure that the dependent tables are created/populated first.
So, is it possible to export a db via phpMyAdmin with the tables in a specific order?
EDIT: Here's the error I'm getting which should clarify things:
Error
SQL query: Documentation
--
-- Dumping data for table `j1_content_rating`
--
-- --------------------------------------------------------
--
-- Table structure for table `j1_core_acl_aro`
--
CREATE ALGORITHM = UNDEFINED DEFINER = `bookings_bpjms`#`localhost` SQL SECURITY DEFINER VIEW `bookings_bpjms`.`j1_core_acl_aro` AS SELECT `bookings_bpjms`.`js0_core_acl_aro`.`id` AS `id` , `bookings_bpjms`.`js0_core_acl_aro`.`section_value` AS `section_value` , `bookings_bpjms`.`js0_core_acl_aro`.`value` AS `value` , `bookings_bpjms`.`js0_core_acl_aro`.`order_value` AS `order_value` , `bookings_bpjms`.`js0_core_acl_aro`.`name` AS `name` , `bookings_bpjms`.`js0_core_acl_aro`.`hidden` AS `hidden`
FROM `bookings_bpjms`.`js0_core_acl_aro` ;
MySQL said: Documentation
#1146 - Table 'bookings_bpjms.js0_core_acl_aro' doesn't exist
The js0_ portions of the import script come after the j1_ portions, and so this error occurs. If I edit this file in a text editor (30+ megs and growing every day) I can find the js0_ portions and move them to the top, but this is tedious, time consuming and error prone.
Is the problem foreign key checks (in which case a SET FOREIGN_KEY_CHECKS=0 at the start of the file should work), or is the problem simply importing in a live environment?
With mysqldump it seems the tables are dumped in the order you give them in (if you specify tables instead of just a database), but this is undocumented behavior as far as I know and hence should not be relied upon.

Mysql 1050 Error "Table already exists" when in fact, it does not

I'm adding this table:
CREATE TABLE contenttype (
contenttypeid INT UNSIGNED NOT NULL AUTO_INCREMENT,
class VARBINARY(50) NOT NULL,
packageid INT UNSIGNED NOT NULL,
canplace ENUM('0','1') NOT NULL DEFAULT '0',
cansearch ENUM('0','1') NOT NULL DEFAULT '0',
cantag ENUM('0','1') DEFAULT '0',
canattach ENUM('0','1') DEFAULT '0',
isaggregator ENUM('0', '1') NOT NULL DEFAULT '0',
PRIMARY KEY (contenttypeid),
UNIQUE KEY packageclass (packageid, class)
);
And I get a 1050 "table already exists"
But the table does NOT exist. Any ideas?
EDIT: more details because everyone seems to not believe me :)
DESCRIBE contenttype
yields:
1146 - Table 'gunzfact_vbforumdb.contenttype' doesn't exist
and
CREATE TABLE gunzfact_vbforumdb.contenttype(
contenttypeid INT UNSIGNED NOT NULL AUTO_INCREMENT ,
class VARBINARY( 50 ) NOT NULL ,
packageid INT UNSIGNED NOT NULL ,
canplace ENUM( '0', '1' ) NOT NULL DEFAULT '0',
cansearch ENUM( '0', '1' ) NOT NULL DEFAULT '0',
cantag ENUM( '0', '1' ) DEFAULT '0',
canattach ENUM( '0', '1' ) DEFAULT '0',
isaggregator ENUM( '0', '1' ) NOT NULL DEFAULT '0',
PRIMARY KEY ( contenttypeid ) ,
Yields:
1050 - Table 'contenttype' already exists
Sounds like you have Schroedinger's table...
Seriously now, you probably have a broken table. Try:
DROP TABLE IF EXISTS contenttype
REPAIR TABLE contenttype
If you have sufficient permissions, delete the data files (in /mysql/data/db_name)
from MySQL Log:
InnoDB: You can drop the orphaned table inside InnoDB by
InnoDB: creating an InnoDB table with the same name in another
InnoDB: database and copying the .frm file to the current database.
InnoDB: Then MySQL thinks the table exists, and DROP TABLE will
InnoDB: succeed.
You may need to flush the table cache. For example:
DROP TABLE IF EXISTS `tablename` ;
FLUSH TABLES `tablename` ; /* or exclude `tablename` to flush all tables */
CREATE TABLE `tablename` ...
I got this same error, and REPAIR TABLE (from #NullUserException's answer) didn't help.
I eventually found this solution:
sudo mysqladmin flush-tables
For me, without the sudo, I got the following error:
mysqladmin: refresh failed; error: 'Access denied; you need the RELOAD privilege for this operation'
(Running on OS X 10.6)
Same problem occurred with me while creating a view.
The view was present earlier then due to some changes it got removed But when I tried to add it again it was showing me "view already exists" error message.
Solution:
You can do one thing manually.
Go to the MySQL folder where you have installed it
Go to the data folder inside it.
Choose your database and go inside it.
Data base creates ".frm" format files.
delete the particular table's file.
Now create the table again.
It will create the table successfully.
I had this problem on Win7 in Sql Maestro for MySql 12.3. Enormously irritating, a show stopper in fact. Nothing helped, not even dropping and recreating the database. I have this same setup on XP and it works there, so after reading your answers about permissions I realized that it must be Win7 permissions related. So I ran MySql as administrator and even though Sql Maestro was run normally, the error disappeared. So it must have been a permissions issue between Win7 and MySql.
I've been fighting with this all day: I have a Perl script that builds a set of tables by first doing a DROP IF EXISTS ... on them and then CREATEing them. The DROP succeeded, but on CREATE I got this error message: table already exists
I finally got to the bottom of it: The new version of MySQL that I'm using has a default engine of InnoDB ("show engine \G;") I changed it in the my.cnf file to default to MyISAM, re-started MySQL, and now I no longer get the "table already exists" error.
I am struggling with the same issue. I cannot create a table, even though it does not exist. I tried all the above solutions with no success.
My solution was to delete the files ib_logfil0, ib_logfile1, ibdata1, and auto.cnf from the data folder of MySQL; make sure to stop the MySQL service first before deleting these files.
Then after restarting the service, MySQL recreated these files and I was able to run a backup script were all my CREATEs were stored (a sqldump file).
Encountering the same problem (create InnoDB table) this is what finally worked for me:
DROP DATABASE `having_issues`;
I checked on a file basis, permissions, tried to REPAIR and FLUSH but nothing worked.
So if this is an option, move all working tables to another DATABASE, drop the old one (you might have to manually remove any files from the database folder before the drop to work), rename the new one, and you 'should' be back on your way. Apparently, whatever gets 'cached' using InnoDB is dropped along with the original database.
You won´t believe me! I´ve just removed a comment block from my .sql file and now it works.
CREATE DATABASE IF NOT EXISTS `issga` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `issga`;
--
-- Table structure for table `protocolo`
--
DROP TABLE IF EXISTS protocolo;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE protocolo (
`idProtocolo` int(11) NOT NULL AUTO_INCREMENT,
`tipo` varchar(30) DEFAULT NULL,
`estado` int(2) DEFAULT 0,
PRIMARY KEY (`idProtocolo`)
) ENGINE=InnoDB AUTO_INCREMENT=142 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `protocolo`
--
LOCK TABLES protocolo WRITE;
/*!40000 ALTER TABLE protocolo DISABLE KEYS */;
/* INSERT INTO `protocolo` VALUES () */
/*!40000 ALTER TABLE protocolo ENABLE KEYS */;
UNLOCK TABLES;
The deleted comment block was this:
--
-- Table structure for table `protocolo`
--
I´ve left the problematic table alone in the same .sql file. After that I´ve removed comments, the only code was left, and the error disappears.
I also encountered this problem where trying to Create a table said it already exists and Dropping the table said it didn't exist.
I did "FLUSH TABLES" and it cleared the problem.
First check if you are in the right database USE yourDB and try Select * from contenttype just to see what is it and if it exists really...
I had the same problem at Mac OS X and MySQL 5.1.40. I used eclipse to edit my SQL script and than I tried MySQLWorkbench 5.2.28. Probably it converted newline characters to Mac format. I had no idea about what's wrong with my script until I commented out the first line in file. After this this script was interpreted by mysql as a one single comment. I used build-in TextEdit Mac application to fix this. After line-breaks was converted to the correct format, the error 1050 gone.
Update for Eclipse users:
To set up default ending for new files created, across the entire workspace:
Window -> Preferences -> General ->
Workspace -> New text file line
delimiter.
To convert existing files, open file for editing and for the currently edited file, go to the menu:
File -> Convert Line Delimiters To
I had this same case. The problem ended up being permissions on the parent directory.
I had been copying files in and out of mysql during testing.
drwx------ 3 _mysql wheel
was not enough, needed to be:
-rw-rw---- 3 _mysql wheel
Sorry to resurrect.
I was having huge issues with Error 1050 and 150.
The problem, for me was that I was trying to add a constraint with ON DELETE SET NULL as one of the conditions.
Changing to ON DELETE NO ACTION allowed me to add the required FK constraints.
Unfortunately the MySql error messages are utterly unhelpful so I had to find this solution iteratively and with the help of the answers to the question above.
Your disk also might just be full. (just had that)
I had this same problem and it looks like the Database name was case sensitive. My Database is called:
Mydatabase
Whilst my script included
USE mydatabase
Once I changed the database name to the correct case it all seemed to work. Using MYSQL Workbench on MAC OSX
This problem also occurs if a 'view' (imaginary table) exists in database as same name as our new table name.
In my case I found this to be an issue with InnoDB; I never discovered what the actual problem was, but creating as a MyISAM allowed it to build
For me the problem was caused when using a filesystem copy of the mysql database directory instead of mysqldump. I have some very large tables, mostly MyISAM and a few InnoDB cache tables and it is not practical to mysqldump the data. Since we are still running MyISAM, XtraBackup is not an option.
The same symptoms as above happened to me. The table is not there, there are no files in the directory that pertain to the table, yet it cannot be created because MySQL thinks its there. Drop table says it's not there, create table says it is.
The problem occurred on two machines, both were fixed by copying backups. However, I noticed that in my backup that there was a .MYD and .MYI file, even though I was under the impression that these files are not used for InnoDB. The .MYD and .MYI files had an owner of root, while the .frm was owned by mysql.
If you copy from backup, check the file permissions. Flush tables might work, but I opted to shut down and restart the database.
Good luck.
gosh, i had the same problem with osCommerce install script until i figured out the mysql system has many databases and the create table query copies itself into each one and thus droping only the working table on active db didnt help, i had to drop the table from all dbs
My CREATE statement was part of staging env dump.
I did try everything that has been mentioned above. I DID NOT get solution. However my path to redemption was:
I stumble upon the fact that (one of many in) the CREATE statement did get through when I rectified the database name case sensitivity. This clicked something. I repeated the same for the other tables.
However a new error came into the scene. The straight quotes for 'comments' were throwing syntax error. I was shocked. replaced them but the new error started popping up. Finally i knew the solution.
SOLUTION: The dump i was using might have been from a different version of MySql. I got permission to connect to the staging MYsql using the local(installed on my machine) mysql workbench. I did not rdp into the staging server to login to staging mysql workbench. Created a dump from there. Ran the dump and it worked like a sweet.
Was trying to import a backup sql file but was getting the error; 1050 "Table already exists"
My setup was:
Windows 7
Mysql 5.5.16
Solution:
Changed the server engine from InnoDB to MyISAM
Using phpMyAdmin Deleted the database I was trying to import to
Restarted the mysql service
Tried the re-importation and it worked
In my case the problem was that there was a view with the same name as my table, so I had to drop the view to allow the import to continue.
drop view `my-view-that-has-same-name-as-table`;
An automated solution that worked for me is to replace the normal drop table with this sed during the dump to also drop any views that might exist:
mysqldump my-db \
| sed -E 's/^DROP TABLE IF EXISTS(.+)$/\0 DROP VIEW IF EXISTS\1/g' \
| mysql my-other-db
Or if you would rather print to a file for backup
mysqldump my-db \
| sed -E 's/^DROP TABLE IF EXISTS(.+)$/\0 DROP VIEW IF EXISTS\1/g' \
> my-db.dump.sql
Or if you received the dumped file and you are importing it to your db
cat my-db.dump.sql \
| sed -E 's/^DROP TABLE IF EXISTS(.+)$/\0 DROP VIEW IF EXISTS\1/g' \
| mysql my-other-db
You get the idea
Note: it is important that you add the ^ at the beginning of the replacement regex, because there are other types of DROP TABLE IF EXISTS commands in dumps that you don't want to touch.
You go from having something like this:
--
-- Table structure for table `my_table`
--
DROP TABLE IF EXISTS `my_table`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `my_table` (
...
To having something like this:
--
-- Table structure for table `my_table`
--
DROP TABLE IF EXISTS `my_table`; DROP VIEW IF EXISTS `my_table`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `my_table` (
...
I've just had the same error but I knew the table already existed and wanted to add to it. I'm adding my answer as this question comes up as no.1 for me on google when looking for the same error but for a slightly different scenario. Basically I needed to tick
"Add DROP TABLE / VIEW / PROCEDURE / FUNCTION / EVENT / TRIGGER statement"
And this solved the error for me.
I had the same error importing a .sql dump for a Magento eCommerce on a new empty database. I've tried some of the solutions above but none worked, I've fixed the problem changing the way the dump was created.
Adding single transaction when creating the dump fixed the problem.
You must delete the files inside the folder of your database in mysql/data/yourdatabase because even though you've deleted/drop table in phpmyadmin the folder's files did not delete.
Needed to a mixed of everything to solve my problem, in this order:
Atention: this solution will erase your broken table's data
Create temporary db (db_temp);
Create a table with SAME NAME and schema in db_temp (twin_table)
Move to db_temp table's folder -> cd /var/lib/mysql/db_temp/
Copy desired table .frm to production_db -> cp twin_table.frm ../production_db/
Change permissions of .ibd and .frm files in production_db-> chmod a+rw twin_table.frm and chmod a+rw twin_table.ibd
Access mysql terminal -> mysql -u root -p
Use production_db -> USE production_db;
Flush tables -> FLUSH TABLES;
Drop broken table -> DROP TABLE twin_table;
re-create table -> CREATE TABLE twin_table (table config goes here);