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.
Related
I can get query for creating a specific table via:
SHOW CREATE TABLE `table_name`
If I do this for all of the tables, I can recreate the whole database by running the gathered sql queries. Is there an easier way to get the table creation script, i.e. sql export file, which created the database just by using CREATE statements?
I would solve this with mysqldump. It's a command-line tool that comes with MySQL. Here's the documentation: https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html
You run it at a command prompt, not in phpMyAdmin. You must have access to a shell environment that is either the same host where you run MySQL Server, or at least can reach that host via the network.
For example:
mysqldump --no-data mydatabase mytable
Output is produced:
--
-- Table structure for table `mytable`
--
DROP TABLE IF EXISTS `mytable`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `mytable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` int(11) DEFAULT NULL,
`c2` int(11) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
(There's more output that I'm omitting.)
You can dump the structure of many tables at once this way, and also views, triggers, procedures, and so on.
If you don't use --no-data, it also includes the data in each table you dump. This makes it an effective tool for data backups. By default, it dumps data in the form of a lot of INSERT statements. So just like the CREATE TABLE statements can recreate the structure of your database if you run them against another MySQL instance, the INSERT statements can recreate your data.
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."
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 try to import a MySQL database on the localhost through phpmyadmin and i receive this error. what it means? and how can i solve it? any ideas?
SQL query:
--
-- Database: `casasdl_mag`
--
--
-- Database: `casasdl`
--
-- --------------------------------------------------------
--
-- Table structure for table `admin_assert`
--
CREATE TABLE IF NOT EXISTS `admin_assert` (
`assert_id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Assert ID',
`assert_type` VARCHAR( 20 ) DEFAULT NULL COMMENT 'Assert Type',
`assert_data` TEXT COMMENT 'Assert Data',
PRIMARY KEY ( `assert_id` )
) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = 'Admin Assert Table' AUTO_INCREMENT =1;
MySQL said: Documentation
#1046 - No database selected
When you create a table you need to select a database for that table to insert into
USE databaseName;
run this before your script
USE casasdl;
CREATE TABLE IF NOT EXISTS `admin_assert` (
`assert_id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Assert ID',
`assert_type` VARCHAR( 20 ) DEFAULT NULL COMMENT 'Assert Type',
`assert_data` TEXT COMMENT 'Assert Data',
PRIMARY KEY ( `assert_id` )
) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = 'Admin Assert Table' AUTO_INCREMENT =1;
Like you can read in error, you have to select your database.
Add USE yourDatabase; at the beginning of the code.
mysql or each db backend should import your tables into a Database , so it needs you introduce a db name , indeed you should tell to mysql :
use mydbname;
you can create it from :
mysqladmin -uroot -p create mydbname
mysql -uroot -p mydbname < mysqlfile.sql
if you are using external php, then do not use use database syntax or mysql-select_db("database"); syntax. Instead use mysqli_connect which accept database name.
The USE db_name statement tells MySQL to use the db_name database as the default (current) database for subsequent statements. The database remains the default until the end of the session or another USE statement is issued.
Refer: http://dev.mysql.com/doc/refman/5.6/en/use.html
Another option is within phpmyadmin Select DB from the left sidebar and inside it import will work.
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