I am moving data from my old database to my new one. The old DB uses:
MySQL version 5.5
CHARACTER SET utf8
I created a new database:
MySQL version 5.6
create database MY_DB; -- Same name as old DB
ALTER DATABASE MY_DB default CHARACTER SET utf8mb4;
Create some users, grant some privileges...
I want to create the db tables and add the data using a mysql dump file, but am concerned that the dump file will overwrite settings and variables that have been set in the new DB.
How do I:
Write the dump command such that any wrong / unnecessary settings and the db create statement are not included? This includes DB and table level settings.
or
Create tables and load data from the dump file, as is, and afterwards set all table and DB settings as they should be in the new DB?
These are the create statements and DB settings that I am seeing in the dump file:
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET #OLD_TIME_ZONE=##TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `MY_DB` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `MY_DB`;
DROP TABLE IF EXISTS `MY_TABLE`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `MY_TABLE` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(80) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
Related
I am generating a dump file form mysql using mysqldump -u root -p --databases dbname > dump.sql, and the file generated is as follows:
-- MySQL dump 10.13 Distrib 8.0.21, for osx10.15 (x86_64)
--
-- Host: localhost Database: dbname
-- ------------------------------------------------------
-- Server version 8.0.21
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET #OLD_TIME_ZONE=##TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `dbname`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `dbname` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `dbname`;
--
-- Table structure for table `tbname`
--
DROP TABLE IF EXISTS `tbname`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `tbname` (
`volume_no` int NOT NULL,
`issue_no` int NOT NULL,
`page_no` int NOT NULL,
`category` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL,
`title` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,
PRIMARY KEY (`volume_no`,`issue_no`,`page_no`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = #saved_cs_client */;
But this has many comments and info which I don't exactly need. Is there a way to directly get output without things like /*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */; etc. as despite being comments, these are generating errors when the file is executed from a python
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */; aren't really comments even though they look that way. They are conditional-execution tokens.
however you can skip comments by --skip-comments and you can try --compact as well
I'm using MySQLDump via system calls to export tables from one database and import them (selectively) on another. My issue is that on the source database server, some tables are in a different (encrypted) directory, and on the target server, there is no target Data Directory.
My current command looks something like this:
mysqldump --skip-opt --skip-comments --no-create-db --disable-keys
--extended-insert --quick --create-options --login-path=sourcedb
source_database_name source_table |
mysql --login-path=targetdb target_database_name
On those tables stored on the encrypted disk, the dump looks something like this:
/*!40103 SET #OLD_TIME_ZONE=##TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `AnExampleTable` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`type_id` int unsigned DEFAULT NULL COMMENT 'FK The system type.',
`resource` varchar(64) DEFAULT NULL COMMENT,
`created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ActionSystemPendingId`),
UNIQUE KEY `resource_type` (`type_id`,`resource`),
KEY `created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 DATA DIRECTORY='/mnt/mysqlEncrpyted/';
/*!40101 SET character_set_client = #saved_cs_client */;
/*!40000 ALTER TABLE `AnExampleTable` DISABLE KEYS */;
-- data inserts
/*!40000 ALTER TABLE `AnExampleTable` ENABLE KEYS */;
/*!40103 SET TIME_ZONE=#OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=#OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=#OLD_SQL_NOTES */;
Essentially all I want to do is drop DATA DIRECTORY='/mnt/mysqlEncrpyted/' from the query without having to resort to sed.
This is done (as I said) via system calls, and some tables are fine because they don't live on a special data directory. All I want is a flag that strips it out.
There's no such option. mysqldump uses the output the server generates from SHOW CREATE TABLE to generate its output. It doesn't build the output from scratch.
Use sed. Or pipe through Perl, or a similar tool.
I've use MySQL Dump Distrib 5.1.45 to backup my database. It's generated a file a below:
-- MySQL dump 10.13 Distrib 5.1.45, for Win32 (ia32)
--
-- Host: localhost Database: SG
-- ------------------------------------------------------
-- Server version 5.5.16
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET #OLD_TIME_ZONE=##TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */;
DROP TABLE IF EXISTS `hist`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `hist` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`HistUi` int(11) DEFAULT NULL,
`StdModel` varchar(30) DEFAULT NULL,
PRIMARY KEY (`Id`),
KEY `StdModel` (`StdModel`),
CONSTRAINT `FKA0015AD86A02605F` FOREIGN KEY (`StdModel`) REFERENCES `std` (`Model`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
LOCK TABLES `hist` WRITE;
/*!40000 ALTER TABLE `hist` DISABLE KEYS */;
/*!40000 ALTER TABLE `hist` ENABLE KEYS */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `std`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `std` (
`Model` varchar(30) NOT NULL,
`Owner` varchar(30) DEFAULT NULL,
PRIMARY KEY (`Model`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
But when I try to restore from that file, I fails and show a message such as:
ERROR 1005 (HY000): Can’t create table ‘SG.hist’ (errno: 150)
I try some solutions got from Google search but not work:
Add SET FOREIGN_KEY_CHECKS = 0; on top of dump file
Use mysql.exe from MySql Server 5.5
Use MySql Workbench
...
I wasted too may time on this issue but still can't find the root cause leading to the problem. I cannot use newer mysqldump.exe file to create dump file again, and don't want to change database structure now. A little change in sql dump file is acceptable.
Update:
Bring that file to other PCs which have the same MySQL server and it works fine. I tried to compare my.ini files, even copying my.ini file from another PC doesn't make it worked.
I had the same problem, which I believe was caused by tables that were expected to be created later in the same dump file already existing, perhaps with slightly different definitions. I dropped the database and recreated it from scratch and everything worked fine. Could this be your problem?
so I have a cpanel hosted database. My instructors have given us a script to load the IMDB database into our database. The file is a .sql file and it looks like this...
CREATE DATABASE IF NOT EXISTS `imdb` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `imdb`;
-- MySQL dump 10.13 Distrib 5.6.11, for Win32 (x86)
--
-- Host: localhost Database: imdb
-- ------------------------------------------------------
-- Server version 5.6.13-log
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET #OLD_TIME_ZONE=##TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `actors`
--
DROP TABLE IF EXISTS `actors`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `actors` (
`actorid` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
`sex` enum('M','F') DEFAULT NULL,
PRIMARY KEY (`actorid`),
KEY `name` (`name`(10))
) ENGINE=InnoDB AUTO_INCREMENT=2798731 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `actors`
--
LOCK TABLES `actors` WRITE;
/*!40000 ALTER TABLE `actors` DISABLE KEYS */;
INSERT INTO `actors` VALUES (1378,'Abagnale Jr., Frank','M'),(1837,'Abbasi, Riz','M')
etc..
So I go to import the .sql file on cpanel..
And i'm getting an error
I'm not really sure how to go about doing this. The instructions are very vague and no office hours...
Appreciate the help.
The issue with this database import is that the following line states the database that you are importing data into. It is currently set to imdb.
CREATE DATABASE IF NOT EXISTS `imdb`
In cPanel mysql databases (with database prefixing enabled) will have your username prefixed to the beginning of the database name and the user supplied database name is postfixed. In this case you can look at the top of PhpMyAdmin and see:
Database: gress2_hw2
This means you will need to update your SQL and replace imdb to gress2_hw2for the data to be imported into the database you are currently in like in the example below:
CREATE DATABASE IF NOT EXISTS `gress2_hw2`
Additionally if you remove this line the mysql server will import the data into the database currently selected by PhpMyAdmin.
I had to change to change the script to
CREATE DATABASE IF NOT EXISTS `gress2_hw2` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `gress2_hw2`;
Since that what i had originally named my database.
I'm having trouble restoring from a dump file. The dump file has an option to build the database if it doesn't exist. So why does the script below only works when I have the database.
mysql -u root -p1234 modal_db < c:\wamp\db_backups\modal_db\mdb.sql
Here's the dump file
-- MySQL dump 10.13 Distrib 5.5.8, for Win32 (x86)
--
-- Host: localhost Database: modal_db
-- ------------------------------------------------------
-- Server version 5.5.8-log
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET #OLD_TIME_ZONE=##TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `modal_db`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `modal_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `modal_db`;
--
-- Table structure for table `user_info`
--
DROP TABLE IF EXISTS `user_info`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
`email` varchar(30) NOT NULL,
`password` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `user_info`
--
LOCK TABLES `user_info` WRITE;
/*!40000 ALTER TABLE `user_info` DISABLE KEYS */;
INSERT INTO `user_info` VALUES (1,'[object Object]','[object Object]','[object Object]'),(2,'amaya','etongancheta#yahoo.com','neato'),(3,'yoh','yoh#yahoo.com','ff2cbe54328162379ba1baf57b23f9'),(4,'fure','fure#gmail.com','263bce650e68ab4e23f28263760b9f'),(5,'koroja','koroja#yahoo.com','665b3a3829375f4acbb00858299a08'),(6,'maro','ala#yahoo.com','2f66aed2714edaa599e148990c9d16'),(7,'deg','deg#ymail.com','n'),(8,'mario','super_mario#gmail.com','c1210473c214e0cf5968bf147ed079'),(9,'mizuo','hewajim#y.com','n'),(10,'bakuryu','bakuryu#b.com','b'),(11,'maron','m#yahoo.com','m'),(12,'gatou','g#y.com','g'),(13,'mocha','mo#y.com','mo'),(14,'pizza','pizza#gm.com','gm'),(15,'bbq','bbq#food.com','food'),(16,'salad','salad#s.com','s');
/*!40000 ALTER TABLE `user_info` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=#OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=#OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=#OLD_SQL_NOTES */;
-- Dump completed on 2011-06-12 21:53:48
Please help. How do I restore the database if it has been dropped?
Your MySQL command line is attempting to choose a database, which will fail if the database doesn't exist. You can omit that from your command line arguments as the SQL file is choosing a database itself with USE.
Try:
mysql -u root -p1234 < c:\wamp\db_backups\modal_db\mdb.sql