How to rename database in MySQL without loosing data? - mysql

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

Related

import mysql data dump to Maria DB

I am trying to import mysql data dump to Maria DB with below command
mysql -u root -p --one-database new_db < data_dump.sql;
But I am getting below error
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'mysql -u root -p --one-database zapcheck <
zapcheck.sql' at line 1
I tried different combinations but nothing worked. Its not even telling what's the issue.
Please let me know the issue here or is there any other way I can import?
You can try the command below to import the file:
Note: Open the terminal where dump.sql is located
After opening the terminal:
//Skip this process if you have already created a database.
Mysql> create database newdb;
// Using the new created database
Mysql> use newdb;
// Importing the dump.sql file to newdb database
Mysql> source dump.sql

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

MYSQL: mysqldump returns empty after import

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.

Duplicate MySQL Database in Linux CLI without Exporting an SQL File

I have a MySQL database which I want to duplicate using the Ubuntu Linux CLI without first having to download a MySQL file. I tried the following command:
mysql -uroot -e'mysqldump -uroot db_old | mysql -uroot backup db_new;'
But got this error:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use.
What am I doing wrong? What I'm looking for is for db_new to be created containing the same data as db_old (in other words, copy db_old and name the new database db_new, all in one command without needing to export the data to a file).
First, create the new database
mysql -uroot -pyourpasswd -e "Create database db_new;"
Then run the following (you don't need to execute "-e")
mysqldump -uroot -pyourpasswd -n db_old | mysql -uroot -pyourpasswd db_new;
From the man page:
mysqldump is also very useful for populating databases by copying data
from one MySQL server to another:
shell> mysqldump --opt db_name | mysql --host=remote_host -C db_name
The "-n" option is short for "--no-create-db"
If you don't need a password for your root connection (not recommended), then remove the "-pyourpasswd" from all statements

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