I imported a MySQL dump file into my MySQL server using following command.
mysql> create database database_name;
mysql> use database_name;
In Linux command prompt,
$ mysql -u user_name -p database_name < /tmp/test1.dmp
But, while viewing the database using phyMyAdmin the newly created database_name does not show table relations. It shows only the tables, but not relations.
Is there any set up required before importing the database in mysql database?
How to extract the relations between tables?
I just went through the exact same problem.
Is there any set up required before importing the database in mysql database?
Not exactly, but it seems LAMP Server installed on Ubuntu or any Linux Distribution uses MyISAM storage engine by default while creating tables. On InnoDB supports Foreign Key relation. [more info]
To change the storage engine afterwards. [Source answer]
You have to add the line default-storage-engine = InnoDB under the [mysqld] section of your mysql config file (my.cnf or my.ini depending on your operation system) and restart the mysqld service. I don't believe you can change this through phpMyAdmin.
On ubuntu, my.cnf is located inside /etc/mysql/.
Or, you can use mysql command
mysql> SET storage_engine=InnoDb;
After this, all the tables and database you create after this, will use InnoDB as their default storage engine, thus eliminating the issue afterwards.
How to extract the relations between tables?
After you change default engine of your database. You also have to change the default engine of your tables, because they haven't been changed yet. Use the syntax below to change the storage engine
ALTER TABLE <table_name> ENGINE = innodb
Using phpMyAdmin
Go to the operations tab after selecting a table
Go to the table options [See below]
You will see an option to change the storage engine of the table
Change the storage engine to InnoDb and hit Go
After this, export the database using phpMyadmin or dump your database using mysqldump. it will show the relations.
Related
I have the following case, but no possibility to make a test. Would be thankful for the advice.
A) local host with the MySQL server (an old mysql 5.0)
A.1) This MySQL server has a few databases
A.2) One of these databases contains some innoDB tables, but also a few "ndbcluster" (ENGINE=ndbcluster) ones.
B) remote NDB cluster with a huge set of data used on the local host in A)
My goal is to shrink the DB (remove tabledata files) on the local host.
For this, I will have to drop all the databases and then remove the physical DB files. Then, add innodb_file_per_table to my.cnf and start MySQL server.
The questions:
1) AFAIK when I execute DROP DATABASE db_name, it first removes all the tables inside the DB. How can I ensure that when I drop the database, the tables/data inside the remote NDB cluster is not affected (it is used by other similar local servers)?
2) When I insert the dump back (it will recreate the dropped database) to the refreshed local MySQL server, will it add/connect the ndbcluster tables from the NDB cluster automatically?
local my.cnf has the following lines:
ndbcluster
ndb-connectstring="nodeid=35,1.2.64.69,1.2.64.70"
3) Do I really need to drop all the database to remove the physical files and recreate the DB in the folder per table mode?
DROP DATABASE will remove all tables in the database, both
InnoDB and NDB. So you will have to remove table by table
if you want to keep the NDB tables.
Not sure of the answers relating to InnoDB.
To ensure that I am not touching the NDB tables, I skipped removing the virtual databases. So, the procedure briefly looked like this:
Dump the mysql db and other important DBs
Stop the MySQL service
Move/remove the db files:
cd /var/lib/mysql/ && rm -f ib_logfile0 ib_logfile1 tablespace1 tablespace2
Re-initialize mysql and add the root user (as it would not start normally):
cd /var/lib/mysql && mysql_install_db
Start the mysql server
Insert the mysql db (from dump) using the default root user without a password.
Insert other databases using the old (restored) user.
When the database that includes an NDB table is inserted, the NDB table will be connected automatically.
I had to reinstall MySQL some time ago, before doing it I had moved /var/lib/mysql/mydatabase to another directory to be able to restore it after installation of MySQL.
After I installed MySQL I moved back this directory. When I go to mysql console and use
SHOW DATABASES;
it returns list of databases and 'mydatabase' is among the list.
When I switch to using 'mydatabase' and use
SHOW TABLES;
it shows the list of tables, but when I do any SELECT command I get this error:
ERROR 1146 (42S02): Table 'mydatabase.mytable1' doesn't exist
From the very beginning - was it enough to backup only /var/lib/mysql/< DATABASE_NAME > to restore database data or have I missed something? If yes then what I could try to fix this issue with 'table doesn't exist'?
MySQL version is 5.7, OS is Ubuntu 16.04
MySQL 5.7 default storage Engine is InnoDB. This stores the data in ibdata,ib_logfile0, and ib_logfile1. If you haven't backed up these files then you cannot restore the data.
It is suggestible that instead of moving your database files we should use mysqldump utility.
It is better to use innodb-file-per-table which can store InnoDB tables in a .ibd file per tables.
I run a daily backup mysqldump backup of the production database (mysql version 5.1.66):
mysqldump --user=username --password=secret -C -e --create-options --hex-blob --net_buffer_length=5000 databasename > file
I also do a daily restore of that database on my development machine (mysql version 5.6.12)
mysql --user=username --password=secret databasename < file
I get the error:
ERROR 1813 (HY000) at line 25: Tablespace for table 'databasename.tablename' exists. Please DISCARD the tablespace before IMPORT.
My reading indicates this is because the mysql innodb database requires the command:
ALTER TABLE tbl_name DISCARD TABLESPACE;
to be run before the table is dropped -- it seems that dropping the table isn't sufficient to get rid of its indexes.
(my development server uses the innodb_file_per_table option)
I don't want to use 'replace' option because i could potentially have data in my development database that was deleted on the production database.
btw after the error the tables are not readable, but restarting mysqld fixes it.
So the question is, is there any mysql dump option that will help fix this issue, or is there another way to import the data that will prevent the error?
thanks in advance for reading.
Sounds like you have a tablename.ibd but no tablename.frm.
To check:
cd to your mysql data directory then the database name.cd /var/lib/mysql/database_name
Search for the table name that is giving the error.
ls tablename.*
You should see two files:
tablename.ibd
tablename.frm
But I'm guessing you don't and only see tablename.ibd
To fix you have a few options:
Add the follow to mysqldump, which will cause the database to be dropped, cleaning up data directory, before restore.--add-drop-database
Copy the tablename.frm from prod over to dev and then issue a delete table statement.
Also:
No need to use net_buffer_length=5000 when you're dumping to a file on localhost.
Other backup solutions - Percona Xtrabackup
I found the easiest way to skip this problem was to manually edit phpmyadmin database dump and edit/change the table that had problems to something else than INNODB. I changed the problem table to ENGINE=MyISAM and voila. Import worked.
CREATE TABLE IF NOT EXISTS `home3_acymailing_tag` (
`tagid` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
`userid` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`tagid`),
KEY `useridindex` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
I also encountered that problem while dropping a schema and creating it again. I overcome this issue by going C:\ProgramData\MySQL\MySQL Server 5.6\data\my_database_name and deleting the tables which remained from the previous database creation. You can also delete the entire database, if you wish.
if you are using XAMPP then first ("stop") MySQL
Then go to C:\xampp\mysql\data\dnb
where in my case dnb is my database name folder.
so then open it and delete .ibd file hence you can only delete it when you already stop MYsql .
then go to phpmyadmin
1 click on phpmyadmin .
2 click on databases that appear below (server.127.0.0.1 in your case my be change)
3 then check your database which you want to drop,and click on drop.
4 then you can create database with same name and import your database successfully .here you can see how you drop database from phpmyadmin
Trying to find out how people do a full backup/restore procedure: The user defined database schema and data can be easily backed up via mysqldump, but what about the master tables and data? i.e. if the server goes completely bananas, how can I rebuild the database, i.e. including all the settings in Mysql? is it just a matter of dumping/importing the information_schema and mysql databases + restore my.cnf ? (innodb or MyISAM, not ISAM)
--
edit: Thanks!
You don't back up information_schema, but otherwise, yes, keep a copy of your my.cnf and a dump of the mysql db tables and log settings. To do this do:
mysqldump -u$user -p$pass --all-databases > db_backup.sql
If you're going to restore to the 100% same version of MySQL, you could also backup by shutting down your server and doing a full copy of the contents of /var/lib/mysql (or wherever your data files are) along with your my.cnf file. Then just drop the copy back in place when you want to go live and turn on your server.
I have set the Master DB Name as MDB & in the Slave server I set to replicate-do-db=SDB <-- this did not work? But when I set it up as the same DB name it works. Is there any solution out there to setup 1 master db with 2 different slaves but in the same server??
You need to specify the replicate-rewrite-db option:
--replicate-rewrite-db=from_name->to_name
Tells the slave to translate the default database (that is, the one
selected by USE) to to_name if it was from_name on the master. Only
statements involving tables are affected (not statements such as
CREATE DATABASE, DROP DATABASE, and ALTER DATABASE), and only if
from_name is the default database on the master. This does not work
for cross-database updates. To specify multiple rewrites, use this
option multiple times. The server uses the first one with a from_name
value that matches. The database name translation is done before the
--replicate-* rules are tested.
If you are only replicating certain databases, you will need to specify the replicate-do-db. Note that the argument to this is the name of the database after the rename operation applied by replicate-rewrite-db:
--replicate-do-db=db_name
MySQL Replication with different database names
Add the following to the logging and replication section of your MySQL configuration file (/etc/mysql/my.cnf), I inserted mine right above relay-log.
replicate-rewrite-db = db_1->db_2
Replace db_1 with the database's name being replicated from the remote master and db_2 with the destination database's name.
Restart the MySQL server (/etc/init.d/mysql restart)
Access the MySQL shell (mysql -h localhost -u root -p)
Check the Slave status (SHOW SLAVE STATUS\G)