I'm struggling to do this.
I have created a new database in the terminal called "somedb" using
CREATE DATABASE somedb
On my desktop I have the SQL dump downloaded from phpMyadmin: somedb.sql
I have tried:
somedb < /Users/myname/Desktop/somedb.sql
Result: ERROR 1064 (42000): You have an error in your SQL syntax
mysql -u myname -p -h localhost somedb </Users/myname/Desktop/somedb.sql
Result: ERROR 1064 (42000): You have an error in your SQL syntax;
I'm new to SQL (The purpose of importing this db is for a text book exercise)
I have granted myself all privileges and there is no password.
Any idea what I'm doing wrong?
Here is the top of the SQL dump file:
-- phpMyAdmin SQL Dump
-- version 4.0.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 18, 2013 at 02:22 PM
-- Server version: 5.5.31-30.3
-- PHP Version: 5.2.17
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!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 */;
--
-- Database: `somedb`
--
CREATE DATABASE IF NOT EXISTS `somedb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `somedb`;
-- --------------------------------------------------------
--
-- Table structure for table `actions`
--
CREATE TABLE IF NOT EXISTS `actions` (
`action_id` int(11) NOT NULL AUTO_INCREMENT,
`action` varchar(75) NOT NULL,
`qualifiers` text NOT NULL,
`response` varchar(75) NOT NULL,
`response_vars` text NOT NULL,
`active` tinyint(4) NOT NULL,
PRIMARY KEY (`action_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Stores user defined actions triggered by certain events' AUTO_INCREMENT=3 ;
-- --------------------------------------------------------
--
I found an SO post here.
I used "source" like so:
SOURCE /Users/myname/Desktop/somedb.sql;
That worked. Great but the internet seemed to want me to use the method like so:
mysql -u username -p password databasename < filename.sql
I may post another question on when to use that second method but in the meantime I just used source from a SQL dump file
Using MAMP Pro, created "uploads" dir in MAMP and put my SQL file in there called "file.sql". Ran the query below in terminal and worked for me.
Make sure to replace brackets and and user info with no spaces after "-u" or "-p"
/Applications/MAMP/Library/bin/mysql -u<username> -p<root> <db_name> < /Applications/MAMP/uploads/file.sql
You can try this way..
Go to the mysql prompt and then type the following.
mysql> \. <path> /filename.sql
Note the gap between . and the path of the sql file.Hope this works.
Type this on terminal
sudo mysql -p database_name < folder/database_dump_file.sql
it will then ask you for the mysql password
mysql -uUser -p --default-character-set=utf8 databasename < /<path to .sql>
(-p) will ask for password
Related
I have a script on server that should backup all databases, but it saves all views as tables. I suppose that I have a keys for mysqldump wrong. Can anyone help me to get them right?
script:
mysql -uroot -pxxx -e "SHOW DATABASES;" > dblist.txt
exec < dblist.txt
read dbname1
while read dbname
do
mysqldump -uroot -pxxx -r ${dbname}.sql --routines --add-drop-database --default-character-set=cp1251 --create-options --complete-insert $dbname || echo -e "${dbname} backup failed!"
done
Output:
--
-- Temporary table structure for view `Events`
--
DROP TABLE IF EXISTS `Events`;
/*!50001 DROP VIEW IF EXISTS `Events`*/;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
/*!50001 CREATE TABLE `Events` (
`id` tinyint NOT NULL,
`name` tinyint NOT NULL,
`subname` tinyint NOT NULL,
`description` tinyint NOT NULL,
...
`subname_e` tinyint NOT NULL
) ENGINE=MyISAM */;
SET character_set_client = #saved_cs_client;
What actually in DB:
MySQL [(none)]> show create table `Events`\G;
*************************** 1. row ***************************
View: Events
Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`#`localhost` SQL SECURITY DEFINER VIEW `db`.`Events` AS select `e`.`id` AS `id`,`e`.`name` AS `name`,`e`.`subname` AS `subname`,`e`.`description` AS `description`, ... `e`.`subname_e` AS `subname_e` from (`db1`.`Events` `e` left join `db2`.`EventsSitesLogo` `s` on((`e`.`id` = `s`.`eventid`)))
character_set_client: utf8
collation_connection: utf8_unicode_ci
1 row in set (0.00 sec)
mysql-server version: 5.1.73
EDIT1: I stumbled upon information that the table I see in dump-file is a temporary table that is meant to ensure that views that are made on the base of other view(s) are made correctly, while actual algorithms for views' creation are always at the end of backup. However, my file ends with these lines:
/*end of normal tables dump*/
--
-- Dumping routines for database 'db'
--
Nothing more. No views. Please help.
Ok, the thing that helped me in the end was this line:
mysqldump --add-drop-database --add-drop-table --allow-keywords -QqceKf --routines --create-options --flush-privileges --insert-ignore -r /home/kov/db.sql -uroot -pxxx db
I am not sure what option helped in the end, but I suppose it is either --allow-keys or -K. I will probably investigate it later if I'll have a time. If I do I'll be sure to share my findings.
I recently dumped a database as part of migrating it from an old server to a new server. Some of the tables have the utf8mb4 character set as the default collation and as the character set for some of the fields.
One of the tables also has a field of type POINT. When I run mysqldump --default-character-set=utf8mb4 ... > dump.sql and then try to import dump.sql I get the "Invalid utf8mb4 character string" warning for any rows that have a location set in the POINT field.
Do I need to worry about this? It looks to me like the data gets migrated correctly, but I do so dislike seeing warnings!
EDIT: Sample code and how to reproduce. The following is the output of mysqldump with no special options (adding the default utfmb4 charset option didn't help), on an example table of an example database. The table has one row:
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `Items` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
`LatLong` point NOT NULL,
PRIMARY KEY (`ID`),
SPATIAL KEY `LatLong` (`LatLong`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
INSERT INTO `Items` VALUES (1,'item1','\0\0\0\0\0\0\0?4??=#\??O#?V?');
If you run from with mysql source file.sql on the file containing this SQL you get the following warnings on the insert:
Query OK, 1 row affected, 1 warning (0.00 sec)
Warning (Code 1300): Invalid utf8mb4 character string: 'BE141E'
Further information:
> file -i file.sql
file.sql: application/octet-stream; charset=binary
> mysql --version
mysql Ver 14.14 Distrib 5.7.17, for Linux (i686) using EditLine wrapper
Running Ubuntu 16.04 LTS
I think this is another symptom of https://bugs.mysql.com/bug.php?id=80150 .
Fixed in MySQL 5.7.23, 8.0.12. Changelog: "If mysqldump or mysqlpump were used to dump binary data without the --hex-blob option, reloading the dump file could produce spurious warnings (values were inserted correctly regardless of the warnings). Such values are now written preceded by the _binary introducer to
silence the warnings."
This question has been asked many times but none of the help offered has solved my problem, possibly because as a newby I am not correctly applying the answers.
The code below was produced by accessing the on-line databases I am developing for the local church(where the various PHP pages call the data correctly and generally do what I expected) and using the phpMyAdmin Export command.
My original development area running on a localserver on my PC failed, so I created a new local server, using phpMyAdmin. I created the databases I needed - terrier and graveyard. No tables as yet. I then ran the file I had obtained on-line by the Export command.
I expected the file produced by Export would work when I run it on the local server using Import. But I get the #1062 - duplicate entry '1' for key 'primary' (or variation on that message) whatever I try
I have only included the graveyard file as far as the admin section. I never get as far as the next section! The terrier one is identical apart from tables and data and produces the same error.
Thanks.
Ian
-- phpMyAdmin SQL Dump
-- version 4.2.5
-- http://www.phpmyadmin.net
--
-- Host: localhost:3306
-- Generation Time: Mar 18, 2015 at 01:17 PM
-- Server version: 5.0.95-log
-- PHP Version: 5.5.14
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!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 */;
--
-- Database: `graveyard`
--
-- --------------------------------------------------------
--
-- Table structure for table `admin`
--
CREATE TABLE IF NOT EXISTS `admin` (
`adminId` int(11) NOT NULL auto_increment,
`userName` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `admin`
--
INSERT INTO `admin` (`adminId`, `userName`, `password`) VALUES
(1, 'wardens', 'Bega&1120');
Apperently you are importing the data into an already populated table.
Notice that there is no USE command. What controls which database (terrier or graveyard) the INSERTs will go into?
If that does not ring a bell, walk through the dataflow in more detail. Think about providing your grandmother a numbered list of instructions to follow.
Background Information
While doing a backup / restore with mysql, I noticed that the restored database was missing a bunch of tables.
Code:
To create the dump file, this is the command that is being used:
mysqldump --databases widgetdb --master-data -u username -ptest --add-drop-database --extended-insert > /var/test/dump.db
Then we are trying to restore the database onto a different server using the following command:
mysql --user=username--password=test widgetdb < /var/test/dbdump.db
So I've checked the dump file and I searched for a table that's missing in the restored database. This is what the code looks like:
DROP TABLE IF EXISTS `widget1`;
/*!50001 DROP VIEW IF EXISTS `widget1`*/;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
/*!50001 CREATE TABLE `widget1` (
`id` tinyint NOT NULL,
`name` tinyint NOT NULL,
`label` tinyint NOT NULL,
`objtype_id` tinyint NOT NULL,
`asset_no` tinyint NOT NULL,
`has_problems` tinyint NOT NULL,
`comment` tinyint NOT NULL
) ENGINE=MyISAM */;
SET character_set_client = #saved_cs_client;
I found the following post: MySQL dump file and commented out lines
This seems to indicate then that the commented out lines will still be run. In my case, I'm running mysql version 5.5.35 which is higher than 50001...
But the table isn't created for me when I restore the dump file.
What I've Tried So Far:
I've tried to change the command use to create the dump file so that I use the --opt option in an attempt to use the "defaults". The restored database is still missing tables.
I'm trying to use mysqldump to export only the DB schema -- no data, no additional SQL comments, just the CREATE TABLE commands. Here's what I've got so far:
mysqldump -h localhost -u root -p --no-data --compact some_db
It almost achieves what I want, but I'd like to eliminate the "character set" lines (those like the first 3 lines in the example output below). Is there a mysqldump option to do that?
/*!40101 SET character_set_client = #saved_cs_client */;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `foo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bar_id` int(11) DEFAULT NULL,
`bazz` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=369348 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `bar` (
...etc.
Here's my version info, in case that matters:
mysqldump Ver 10.13 Distrib 5.1.34, for Win32 (ia32)
mysql Ver 14.14 Distrib 5.1.34, for Win32 (ia32)
This uses grep as well, but it seems to work:
mysqldump -d --compact --compatible=mysql323 ${dbname}|egrep -v "(^SET|^/\*\!)"
I'm using:
Ver 10.11 Distrib 5.0.51a, for
debian-linux-gnu (x86_64)
Here is the command to dump the schema without the character set and AUTO_INCREMENT.
mysqldump -h localhost -u root -p --no-data YOUR_DATABASE_HERE |egrep -v "(^SET|^/\*\!)" | sed 's/ AUTO_INCREMENT=[0-9]*\b//'
Here is the command to dump the schema without the character set, AUTO_INCREMENT and the comments
mysqldump -h localhost -u root -p --no-data --compact YOUR_DATABASE_HERE |egrep -v "(^SET|^/\*\!)" | sed 's/ AUTO_INCREMENT=[0-9]*\b//'
mysql> SHOW CREATE TABLE mytablename;
mysqldump --compact --no-set-names --skip-opt --no-data DB | sed "/ SET /d"
Did you try the --skip-comments option mentioned in the manual? Does it help?
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_comments
Use --skip-set-charset option.
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_set-charset