XAMPP Retore Bulk Databases with CLI - mysql

On practice the way to retore MySQL or DBMaria Database from file "dump.sql" is this:
mysql --user=root --password="" --database=myblog < bitnami_wordpress.sql
But what do I do if they are multiple databases in File, any comand Line to upload this without pointing to a database?, becouse database is in Dump File like this:
DROP DATABASE IF EXISTS `myblog `;
CREATE DATABASE IF NOT EXISTS `myblog ` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `myblog `;
i use PHPMyAdmin to crerate the dump.sql file; but i want to upload via CLI

Related

Update another table in MySQL when i use "source" command line

I have two database and i want to import/export form one to other with source command line.
The names of my databases is melka and demelka.
I want to copy form melka to demelka. for this i follow this steps:
Export from melka DB and save in D:/db/melka.sql and then run this command line
cd D:\wamp64\bin\mysql\mysql5.7.24\bin
mysql -u root
USE demelka;
source D:/db/melka.sql
But after executing the above commands, melka Table is updated And demelka Table is empty.
Environment:
I use WAMP 3.1.7
and mysql5.7.24
Thanks to #madhur-bhaiya
I am open my melka.sql file in editor and deleted the following commands from within and corrected them
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `melka` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `melka`;

Create MySQL database with sql file import in a single line

I have a sql file that I want to upload from the command line. To do this I first need to go into MySQL and create the database
mysql> create database myDB;
and then call the following from the command line:
mysql -u username -p myDB < myDB.sql
Is there a way to do this all in one line from the command line?
I tried adding the following lines (and various combinations) to the beginning of the myDB.sql file:
create database `myDB`;
use `myDB`;
and then running
mysql -u username -p < myDB.sql
but that did not work.
ERROR 1046 (3D000) at line 7: No database selected
.sql files are generally LITERALLY just a sequence of SQL DDL/DML queries. If it was created by mysqldump, then it should contain EVERYTHING needed to recreate the database from the ground up, including the necessary create db and create table queries.
if they're not there, you can manually add them to the top of the file yourself via simple file concatenation/modification, e.g.
echo 'create database foo;' > new.sql
cat otherstuff.sql >> new.sql
mysql < new.sql
If you generated the mysqldump file with the --databases or --all-databases options, then it puts the correct CREATE DATABASE statements into the dump file.

How to Import MySQL dump file with Red Hat 6

I have a VM that I'll be installing mysql server on. I have a dump file that I need to import into mysql. The first line says this:
-- MySQL dump 10.13 Distrib 5.6.20, for linux-glibc2.5 (x86_64)
The file already has databases and tables, along with the structure and data.
grep -i 'current database' db_dump.txt
-- Current Database: `db1`
-- Current Database: `db2`
-- Current Database: `db3`
grep -i 'data for table' db_dump.txt
-- Dumping data for table `TABLE1`
-- Dumping data for table `TABLE2`
-- Dumping data for table `TABLE3`
As you can see, its a .txt file and this is partly where my confusion is coming in. Much of what I have read is that in order to import a text file you must already have the database's and tables created. However they are already defined in the file. So that lead me to running a command such as this:
mysql -u <user> -p < filename.dump
But some of the documentation says you must have a .sql file in order to do this. So can I just rename my .txt file to .sql or just import as is? What would the command look like? I am really a noob when it comes to MySQL so any guidance is much appreciated.
You command is correct. The extension of the filename is not important. It is what's inside that matters.
Oh! There is actually one thing that needs to change:
mysql -u<username> -p database_name < filename.dump
The database name should be the last part of the command and the username needs to go after -u, so if you are the root user you should type:
mysql -uroot -p database_name < filename.dump

Create MySQL Database with .SQL File

I don't know much about MySQL at all. But I am trying to reverse engineer a MySQL database using Visio. I know what steps I should take to do this, but I can't get my database to show in the 'Databases' section (as below):
How do I create the MySQL database using the .SQL file and get it to show up in this list? I have tried this code:
mysql -u username -p password database_name < filename.sql
using my own credentials of course. But that doesn't seem to work. In what folder should the .SQL file be placed if this statement is to work?
1) Create a file "filename.sql"
2) Create a database in your DB in which you want to import this file.
3) From command-prompt/terminal, move to the directory where you have created a "filename.sql".
4) Run the command: mysql -u username -p password database_name < filename.sql. (You can also give the proper path of your file and run this command from anywhere). It might be the case that you don't have a password set for MySQL. If so, mysql -u username database_name < filename.sql will also work.
In your case if you have created a database with name ojs and also created a file with name ojs.sql in C: drive then run the following command:
Edit: Put the path inside quotes.
mysql -u username -p password ojs < "C:\ojs.sql"
There is another way of importing tables in mysql. You can do it this way as well:
1) Connect your database
2) Type command "use ojs;"
3) Type command "source C:/ojs.sql"
Most MySQL SQL files that create databases create the database 'on-the-fly', so you typically needn't do anything except:
log-in
mysql -u [username] -p[password]
(Note: make sure you do NOT include a space (' ') character between the -p and the [password].
MySQL will think that [password] is the name of the database you want to connect to.
The 'general' log-in (above) does not assume you want to connect to any particular schema.)
source the file (do not use quotes around filename)
mysql> source [database_creation_file].sql
you can simply do it using mysql workbench
1> create a new query tab
2> CREATE DATABASE database_name;
3> USE database_name;
4> open the filename.sql file and execute it ctrl + shift + enter
5> all the tables in the filename.sql are created
To create a MySQL database using a SQL file, you can follow these steps:
Log in to your MySQL server using the mysql command-line tool and the appropriate credentials.
Use the CREATE DATABASE command to create a new database with the desired name:
CREATE DATABASE database_name;
Use the USE command to switch to the newly created database:
USE database_name;
Use the SOURCE command to import the SQL file into the database:
SOURCE path/to/sql/file;
The database will now be created and populated with the data from the SQL file. You can verify this by running some SQL queries against the database.
It's important to note that this process assumes that the SQL file contains valid SQL statements compatible with the version of MySQL you are using. If the SQL file contains any errors or unsupported statements, they will be displayed in the mysql command-line tool, and the import process will be interrupted.

Backing up a MySQL database and restoring it under another name

I am trying to do some maintenance on MySQL database data and I created a dump file with the backed up current database.
I want to restore all that data into another database called something like original_db_name_test
Is there a command for that?
This depends on how you invoked mysqldump
If you used mysqldump dbname, then your dump contains neither CREATE DATABASE nor USE DATABASE.
Just create the database with the new name and feed the dump to mysql -D new_dbname.
If you used mysqldump --database dbname, then the dump contains CREATE DATABASE and USE DATABASE statements.
You need to comment them out or replace with new_dbname.
mysql -u usernamehere -p original_db_name_test < yourdumpfilehere.sql
If you used mysqldump to create the dump file, simply:
Create a new database (use the mysqladmin command line tool - "mysqladmin create [new database name]").
Edit the dump file to add a "USE [new database name];" at the top. (There might be an existing use statement that's commented out, so you can change this and un-comment it.)
Import the dump into the new table via "mysql -u <user name> -p < [dump file name]".
Incidentally, when creating a dump via mysqldump, I'd be tempted to use the "--add-drop-table" option, as this will cull any existing table with the same name prior to issuing the table creation statement.
you can use the 'MySQL Workbench' Application and do this with a nice gui