I have a DB which is a live one, what I'm looking to do, is to make a copy.
I have access to MySQl via SSH and phpMyAdmin.
Is there a command where I can copy/backup the DB, in a single command/action, without using export/import?
Thanks
mysqldump -u USERNAME -pPASSWORD databaseName > SAVETOFILE.sql
see this http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html for various options available.
you can do via PHPMyAdmin as well see here http://php.about.com/od/learnmysql/ss/mysql_backup_3.htm
Login to phpMyAdmin
Click on your database name
Click on the tab labeled EXPORT
Select all tables you want to backup (usually all)
Default settings usually work, just make sure SQL is checked
Check the SAVE FILE AS box
Hit GO
If you want to create DB that is a copy of above sqldump you need to do run the following command
mysql -u USERNAME -pPASSWORD < SAVEDFILE.sql
But, I feel you are looking for something like replication. In that case you need to set-up master-slave configuration where data gets replicated on slave. See this guide for replication
http://dev.mysql.com/doc/refman/5.0/en/replication-howto.html
Ok, so I found a command that would take a dump of one database and then insert it into another DB using a single command:
mysqldump -u username -ppassword live_db | mysql -u username -ppassword backup_db
Related
I am exporting database from PHPMYADMIN. Database has 341 tables on remote server but when I import Sql file into local server PHPMYADMIN I got only 213 Tables.
Question :
How can I get all tables of database on localhost.
phpmyadmin should export all when you do export -> quick, but try to use the custom option and make sure all are selected. Also, may want to check the mysql mode (select ##global.sql_mode) of local and remote. If your local has default settings, there may be some tables with data in a column that's not allowed.
Assuming that your database is pretty large then you may have more success with exporting it via the command line rather than phpMyAdmin:
/usr/bin/mysqldump -u USERANME -p DATABASE_NAME > output.sql
Run that, you'll be prompted for your database password and then you'll get output.sql containing your database
You can then import this using ..
/usr/bin/mysql -u USERNAME -p DATABASE_NAME --no-create-db < output.sql
This question already has answers here:
How do I rename a MySQL database (change schema name)?
(46 answers)
Closed 8 years ago.
I created a database with the name of hrms. Now I need to change database name to sunhrm. But, It is disabled in MySQL workbench. Can I do that on the Linux server itself?
In case you need to do that from the command line, just copy, adapt & paste this snippet:
mysql -e "CREATE DATABASE \`new_database\`;"
for table in `mysql -B -N -e "SHOW TABLES;" old_database`
do
mysql -e "RENAME TABLE \`old_database\`.\`$table\` to \`new_database\`.\`$table\`"
done
mysql -e "DROP DATABASE \`old_database\`;"
I don't think you can do this. Basic answers will work in many cases, and in others cause data corruptions. A strategy needs to be chosen based on heuristic analysis of your database. That is the reason this feature was implemented, and then removed. [doc]
You'll need to dump all object types in that database, create the newly named one and then import the dump. If this is a live system you'll need to take it down. If you cannot, then you will need to setup replication from this database to the new one.
If you want to see the commands that could do this, #satishD has the details, which conveys some of the challenges around which you'll need to build a strategy that matches your target database.
It's possible to copy database via mysqldump command without storing dump into file:
mysql -u root -p -e "create database my_new_database"
mysqldump -u root -p original_database | mysql -u root -p my_new_database
mysql -u root -p -e "drop database original_database"
You can create a new database exactly as the previous database existed and then drop the old database when you're done. Use the mysqldump tool to create a .sql backup of the database via mysqldump orig_db > orig_db.sql or if you need to use a username and password then run mysqldump -u root -p orig_db > orig_db.sql. orig_db is the name of the database you want to "rename", root would be the user you're logging in as and orig_db.sql would be the file created containing the backup. Now create a new, empty database with the name you want for the database. For example, mysql -u root -p -e "create database new_db". Once that's done, then run mysql -u root -p new_db < orig_db.sql. new_db now exists as a perfect copy of orig_db. You can then drop the original database as you now have it existing in the new database with the database name you wanted.
The short, quick steps without all the above explanation are:
mysqldump -u root -p original_database > original_database.sql
mysql -u root -p -e "create database my_new_database"
mysql -u root -p my_new_database < original_database.sql
mysql -u root -p -e drop database originl_database
Hope this helps and this is a reliable means to accomplish it without using some ad-hoc method that will corrupt your data and create inconsistencies.
You can do it by RENAME statement for each table in your "current_db" after create the new schema "other_db"
RENAME TABLE current_db.tbl_name TO other_db.tbl_name
Source Rename Table Syntax
In short no. It is generally thought to be too dangerous to rename a database. MySQL had that feature for a bit, but it was removed. You would be better off using the workbench to export both the schema and data to SQL then changing the CREATE DATABASE name there before you run/import it.
I used following method to rename the database
take backup of the file using mysqldump or any DB tool eg heidiSQL,mysql administrator etc
Open back up (eg backupfile.sql) file in some text editor.
Search and replace the database name and save file.
Restore the edited SQL file
If your DB contains only MyISAM tables (do not use this method if you have InnoDB tables):
shut down the MySQL server
go to the mysql data directory and rename the database directory (Note: non-alpha characters need to be encoded in a special way)
restart the server
adjust privileges if needed (grant access to the new DB name)
You can script it all in one command so that downtime is just a second or two.
For impatient mysql users (like me), the solution is:
/etc/init.d/mysql stop
mv /var/lib/mysql/old_database /var/lib/mysql/new_database
/etc/init.d/mysql start
First backup the old database called HRMS and edit the script file with replace the word HRMS to SUNHRM. After this step import the database file to the mysql
Another way to rename the database or taking image of the database is by using Reverse engineering option in the database tab. It will create a ERR diagram for the database. Rename the schema there.
after that go to file menu and go to export and forward engineer the database.
Then you can import the database.
I have to delete some table data in prod. db and for the records that are going to be deleted a backup of records should be copied to another local db. This involves two databases, residing in two different servers/instances.
Is it possible to do via sql (mysql) query to do this?
I would use mysqldump with a where condition to get the records out. Once you have everything saved, you can then delete them from prod. These commands should work from the command line, including the password to avoid the prompt is optional.
mysqldump -u user -pPassword -h hostname1 dbname tablename
--where 'field1="delete"'
--skip-add-drop-table --no-create-db --no-create-info > deleted.sql
mysql -u user -pPassword -h hostname2 dbname < deleted.sql
mysql -u user -pPassword -h hostname1 dbname -e 'DELETE FROM tablename WHERE field1="delete"'
I'm trying to do exactly the same thing, copy data from a table to another server, then delete it from the original.
So far I see two options:
copy data to a local database then replicate that database to another server
use Federated storage engine
Both require some serious reconfiguration of our servers as neither Federated or binary logging (required for replication) are not enabled. This would take time and it would be best if other solutions could be found.
The process needs to be executed on a daily basis, so it needs to be fully automated.
Perhaps a third option is to automate things with a cron job:
copy the data to a separate database on the same server
backup that database with mysqldump in a folder which is linked on the other server too
on the second server, restore the database from the sql dump
I want to copy all the tables, fields, and data from my local server mysql to my hosting sites mysql. Is there a way to copy all the data? (It's only 26kb, very small)
In phpMyAdmin, just export a dump (using the export) tab and re-import it on the other server using the sql tab.
Make sure you compare the results, I have had phpMyAdmin screw up the import more than once.
If you have shell access to both servers, a combination of
mysqldump -u username -p databasename > dump.sql
and a
mysql -u username -p databasename < dump.sql
on the target server is the much more fast and reliable alternative in my experience.
Have a look at
Copying MySQL Databases to Another Machine
Copy MySQL database from one server to another remote server
Please follow the following steps:
Create the target database using MySQLAdmin or your preferred method. In this example, db2 is the target database, where the source database db1 will be copied.
Execute the following statement on a command line:
mysqldump -h [server] -u [user] -p[password] db1 | mysql -h [server]
-u [user] -p[password] db2
Note: There is NO space between -p and [password]
I copied this from Copy/duplicate database without using mysqldump.
It works fine. Please ensure that you are not inside mysql while running this command.
If you have the same version of mysql on both systems (or versions with compatible db file sytsem), you may just copy the data files directly. Usually files are kept in /var/lib/mysql/ on unix systems.
I have a database and want to copy its tables, etc, to another database, so I can use one of these databases as a "staging" DB.
What is the best/most foolproof way to do this?
Thanks
To be system agnostic... from any commandline:
sqldump -u username -p databasename > backup.sql;
sql -u username -p
<<provide password and access the sql commandline>>
create database newdatabasename;exit;
sql -u username -p newdatabasename < backup.sql
This should build a new database based on a backup of the older one
If you want to copy everything, I'd create a full backup of the DB you want to copy: Right click db > Tasks > Backup. Create an new db in SQL server and restore the backup file into it: right click > Tasks> Restore database. This will give you a full copy of the orignal DB.
You can just use: Right Click Database -> Tasks -> Copy Database...