MYSQL: mysqldump returns empty after import - mysql

I need some help, why is it that after importing an mysqldump (table) at first you can see result, but when you exit
mysql -uroot -proot
and select again the table then check, it returns empty.

first connect mysql by below command-
mysql -uroot -proot
Note: assuming root is password of root user.
Now connect to database in which you imported table-
use my_db;
Now check your table by-
show tables;
or
show tables like 'my_table'
If still getting error then show how you import data and show first few lines of your backup if possible.

Or start mysql command shell using:
>mysql -u youruser -p yourdatabase
and then check up your table.
mysql>select * from yourtable;
P.S If you didn't choose any database you get appropriate error 1046:
No database selected.
If you don't get such error message and see you table empty YOU CHOSE WRONG DATABASE.

Related

importing mysql .sql file appears to randomly selects which tables to import or not

I have database A. I issue this command against it:
mysqldump --host=localhost -uroot -p"mypassword" my_db_name > file.sql
now I take this file to machine B, running mysql too. I create a database:
create database newdb;
I then:
mysql --host=localhost -uroot -proot newdb < file.sql
My problem is that not all tables that exist in file.sql are created in the new database! I clearly see CREATE TABLES users in the content of the file.sql followed by thousands of INSERT calls for content in that table.
But users table is never created in the new database. I am completely lost as to why.
If you have foreign keys, the tables might be created in the wrong order and since the constraints can't be created, creating the table fails. Try adding SET FOREIGN_KEY_CHECKS=0 in the beginning of the dump and SET FOREIGN_KEY_CHECKS=0 at the end.
Delete whole newdb database;
Restart mysqld;
Run mysqlcheck --repair --all-databases -u root -p root on machine B;
Create newdb again (or maybe call it newdb2 just to be sure);
Delete file.sql on machine B, copy file.sql again from machine A and import by mysql --host=localhost -uroot -proot newdb < file.sql;
Run SHOW engine innodb STATUS; and or show table status and analyze results.
Copy a CREATE TABLE that failed to work. In the commandline tool "mysql", paste that. What messages, if any do you get? Does it create the table?
Please provide that CREATE for us; there may be some odd clues.
Also provide SHOW VARIABLES LIKE '%enforce%';

Importing a sql file to mysql on an ubuntu server failing

I'm trying to import a sql file that is on the server using the following command in the terminal
mysql -u NAME -p DBNAME < path/to/FILE.sql
when i do that it asks for the password, after inputting the password nothing happens. I check the database and no tables have been added.
Note:
DBNAME is created in the mysql database.
I have also tried the following syntax in mysql and also that didn't work
mysql> source PATH/TO/FILE.sql
Please Help :D
The sql file was corrupt, all i had to do was delete it and upload it once again. thanks #CBroe
Hello you can trying with
Create the database for import the tables:
mysql -u root -p
create database database_name;
exit
mysql -u user -p databasename < databasename.sql

Copying all tables from one database to another in MySQL

Im trying to copy all my tables from one database to another just using mySQL. I have a code that I've been trying to use which dumps the tables into a sql file which i will then import into another database:
mysqldump -u root -p[root-password] [database] > [sqlfile].sql
so my code would be:
mysqldump -u root -pMyNewPass brochureSite > brochure.sql;
yet Im getting a syntax error when I trying to perform this.
The error I receive:
Write it like
mysqldump -u root -p brochureSite > brochure.sql
Enter when you enter you will be asked for password then enter password.

How to rename database in MySQL without loosing data?

I am trying to rename my database by the following query:
RENAME DATABASE my_db TO newDB;
but its showing me the following error response:
Error Code: 1064. You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'DATABASE
activation_server_db TO activationserver' at line 1
Please help me find where I am going wrong?
Use these few simple commands
mysqldump -u username -p -v olddatabase > olddbdump.sql
mysqladmin -u username -p create newdatabase
mysql -u username -p newdatabase < olddbdump.sql
or For InnoDB, the following seems to work: create the new empty database, then rename each table in turn into the new database:
RENAME TABLE old_db.table TO new_db.table;
You will need to adjust the permissions after that.
I follow these simple steps:
Create new database
Backup the old database
Restore old database under new database
You can use mysqldump
using mysqldump
mysqldump [OPTIONS] --database oldSchema > oldSchema.sql
mysql new_schema < oldSchema.sql
You need to create a dump of your db and then create a new db with different name with that dump.
If it is online you need to take ofline it for avoiding data loss

ERROR 1049 (42000): Unknown database 'mydatabasename'

I am trying to restore database from .sql file , i have created the database in phpmyadmin and also using the create if not exist command in the .sql file which i am restoring to the database and both names of database are same in phpmyadmin and .sql file which is"mydatabase".
Here is the command which i am using to restore database.
mysql -uroot -pmypassword mydatabase<mydatabase.sql;
When i execute the above command i am getting the following error, i have also given all the permission to the user upon this database.
ERROR 1049 (42000): Unknown database 'mydatabasename'
If dump file contains:
CREATE DATABASE mydatabasename;
USE mydatabasename;
You may just use in CLI:
mysql -uroot –pmypassword < mydatabase.sql
It works.
Whatever the name of your dump file, it's the content which does matter.
You need to check your mydatabase.sql and find this line :
USE mydatabasename;
This name does matter, and it's the one you must use in your command :
mysql -uroot -pmypassword mydatabasename<mydatabase.sql;
Two options for you :
Remove USE mydatabasename; in your dump, and keep using :
mysql -uroot -pmypassword mydatabase<mydatabase.sql;
Change your local database name to fit the one in your SQL dump, and use :
mysql -uroot -pmypassword mydatabasename<mydatabase.sql;
Open the sql file and comment out the line that tries to create the existing database and remove USE mydatabasename and try again.
You can also create a database named 'mydatabasename' and then try restoring it again.
Create a new database using MySQL CLI:
mysql -u[username] -p[password]
CREATE DATABASE mydatabasename;
Then try to restore your database:
mysql -u[username] -p[password] mydatabase<mydatabase.sql;
I solved because I have the same problem and I give you some clues:
1.- As #eggyal comments
mydatabase != mydatabasename
So, check your database name
2.- if in your file, you want create database, you can't set database that you not create yet:
mysql -uroot -pmypassword mydatabase<mydatabase.sql;
change it for:
mysql -uroot -pmypassword <mydatabase.sql;
Create database which gave error as Unknown database,
Login to mysql shell:
sudo mysql -u root -p
create database db_name;
Now try restoring database using .sql file, -p flag will ask for a sql user's password once command is executed.
sudo mysql -u root -p db_name < db_name.sql
La Chi's answer works for me.
You can view his/her answer in the comment of zessx answer in this page. But I initially have a problem with it if you also do just tweak his/her answer like this: mysql -h localhost -u root -p -D mydatabase < mydatabase.sql.
Also I would suggest that in the mydatabase.sql portion you include the direct location of it in your computer like "C:\Users\username\desktop".
Thank you.
If initially typed the name of the database incorrectly. Then did a Php artisan migrate .You will then receive an error message .Later even if fixed the name of the databese you need to turn off the server and restart server
I had the same issue, i run this command on command line and just like you i had added the ';' at the end. Removing it solved the issue.
Instead of this
mysql -uroot -pmypassword mydatabase<mydatabase.sql;
try this
mysql -uroot -pmypassword mydatabase<mydatabase.sql
I found these lines in one of the .sql files
"To connect with a manager that does not use port 3306, you must specify the port number:
$mysqli = new mysqli('127.0.0.0.1','user','password','database','3307');
or, in procedural terms:
$mysqli = mysqli_connect('127.0.0.0.1','user','password','database','3307');"
It resolved the error for me . So i will suggest must use port number while making connection to server to resolve the error 1049(unknown database).
mysql -uroot -psecret mysql < mydatabase.sql
I meet your issue. This is how to solve it
Check your DB name correct and exist in MySQL
Check if your IP and port is correct
It works by creating database and than typing command as :
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -u root -p -D cricket < C:\Users\habib_s9ayvfl\Desktop\sqlfile.sql
Create database:
CREATE DATABASE mydatabasename;
USE mydatabasename;
use this one:
mysql -u root -p 'mydatabasename'< '/tmp/db_dump.sql'
Its very simple: Use mysql -u root -p mysql
first, you need to check the folder /var/lib/mysql for mydatabasename (depend on how you installed mysql, but default folder is this one),
please check the folder exists or not and its owner should be mysql:mysql, and of course the folder permission should be rw to mysql;
second, possibly because of you made changes to /etc/my.cnf, for example in my case, we created a database TEST_DB in uppercase, and then someone added lower_case_table_names=1 restriction in my.cnf, it caused the Unknown database error because mysql will transalte TEST_DB to lowercase test_db even when i key in select from TEST_DB, so it'll never find TEST_DB, simply comment out and restart mysql service solved my issue
You can also try
> mysql mysql
and you will connect to MySQL database from which you can create your own schema.
mysql> CREATE DATABASE mydb; USE mydb;
when u import database from workbench or other method ,should be give same name as your dump to avoid this kind of error