mysqldump file - create table command not being executed - mysql

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.

Related

phpMyAdmin export database creation script without alter statements

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.

View definitions in MariaDB are not create with mysqldump

I have a db in MariaDB 10.1.25 and in this, I have many tables and 20 views.
When I try to backup my db using mysqldump, it works fine for tables but in view definitions, it fails to create a create statement like it does with tables.
The code generated is this:
--
-- Temporary table structure for view `qry_clientes`
--
DROP TABLE IF EXISTS `qry_clientes`;
/*!50001 DROP VIEW IF EXISTS `qry_clientes`*/;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
/*!50001 CREATE TABLE `qry_clientes` (
`Id` tinyint NOT NULL,
`Cliente` tinyint NOT NULL,
`Direccion` tinyint NOT NULL,
`Ciudad` tinyint NOT NULL,
`Fono` tinyint NOT NULL,
`Fax` tinyint NOT NULL,
`Email` tinyint NOT NULL,
`Ruc` tinyint NOT NULL,
`tipo` tinyint NOT NULL
) ENGINE=MyISAM */;
SET character_set_client = #saved_cs_client;
and in this there are no view definitions. I have all the privilegies grandted
Usually, in the mysqldump backup script, the views are first created as tables and then are then dropped at the bottom of the script as each view is being created.
Sometimes there is an error in this process because when a view is created there is a user used as DEFINER. This statement may fail because this user might not exist in the database.
Please verify that the view drop/create script exists at the end, write the error that you are getting (if you are getting) and run the import using the -v option for more logging.

mysqldump creates Table instead of a View

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.

Importing a MySQL database

Trying to import a MySQL database using PHP-Admin. When I try to import the file backed-up by my host, I get this error message:
SQL query:
DROP TABLE IF EXISTS `wp_commentmeta`
MySQL said: Documentation
#1046 - No database selected
I have also tried to export the database myself, then trying to import that specific file. When I do that, however, I get a completely different error message:
SQL query:
CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
`meta_id` bigint(20) unsigned NOT NULL,
`comment_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`meta_value` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
MySQL said: Documentation
#1046 - No database selected
What can I do to resolve this?
You need to USE a database. You could edit your file and add the line at the top, something like:
USE databasename;
Some tools will let you set a default database when restoring from backup. If you haven't selected one that could be a problem.
If you haven't created a database you'll need to do that first.
You need to select which database you are importing to in your SQL. Your first statement should be:
USE DATABASE `db_name`;
You have no database selected.
Find which databases exist
SHOW DATABASES:
and then select one for use
USE `database_name`
You say that you are importing into a new host, so I would assume that you do not already have a database created;
CREATE DATABASE `database_name`
Now you can USE that database you just created; and your queries should now work.

Importing a SQL db into mysql using the terminal

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