Importing a database using SSH - mysql

apologies for how simple this question will be to followers of the SQl and SSH tags.
I would like to upload a copied database into a new database.
I created a copy of a data base I'd like to duplicate into my root folder. My root folder now contains a file "sitename_duplicate" with all the data.
In my web host control panel I created a new database "20130924_sitename"
I cannot seem to upload the contents of sitename_duplicate into 20130923_sitename.
mysql -u myname -p sitename_duplicate < 20130923_sitename.sql
-bash: 20130923_sitename.sql: No such file or directory
mysql -u myname -p 20130923_sitename < sitename_duplicate.sql
-bash: sitename_duplicate.sql: No such file or directory
The database with the data is sitename_duplicate and it's in the root. The blank and newly created database is 20130923_sitename.
How do I move the contents of sitename_duplicate into 20130923_sitename using SSH?

The syntax is
mysql -u username -pPassword --host hostname database_name < dump.sql
So it should be for you:
mysql -u myname -pMyPassword 20130924_sitename < sitename_duplicate

Related

How do i restore a MYSQL .dump file?

I have file.sql, and I want to restore it. I found this command:
mysql -u username -p database_name < file.sql
Where should I put 'file.sql' in file system? I'm using Windows and XAMPP. Thank you.
*) The dump file is 3GB++
you should navigate to dump file location and run mysql -u username -p database_name < file.sql
for example if your file is at c drive you can open terminal type cd \ (this will go to root folder if your windows is installed on c dirve) and run the above command

How to transfer my Dump(.sql) file on server?

I have created a dump file for my database and now I want to put that
file on my server to get its access. I will copy that .sql file on my
server, but I don't know how to access that dump file using commands.
Assuming you are using command line
cd /path/where/sql/file/is
And then
mysql -u username -p database_name < file.sql
If you don't have the mysql bin in your PATH you might want to run
/path/to/mysql/bin/mysql -u username -p database_name < file.sql
Or temporarily put the file.sql in the mysql bin directory (not recommended)
Another possible scenario is to point to the file in the filesystem like this
mysql -u username -p database_name < /path/where/sql/file/is/file.sql
If you're on windows you might wanna change the forward slashes to backslashes.
If you want to restore MySQL database on remote-server, then very first thing is that, the .sql file is not necessarily present on server.
We can restore the backup right from our development machine(using mysql.exe of local machine), provided that you have MySQL user having MySQL Server remote access.
Use "-h" option while restoring the backup. "-h" implies hostname where MySQL Database Server is present. It can be in local network or on remote server.
mysql -h www.yourserver.com -u username -p database_name < file.sql
Or with IP Address (use actual IP Address of your Database Server)
mysql -h 112.112.112.112 -u username -p database_name < file.sql
And if you do not have remote-access enabled user, then you need to upload .sql file to remote-server and restore it by putting host-name as localhost. Like,
mysql -h localhost -u username -p database_name < file.sql
Hope it helps, thanks.

Copy all the Database - Tables,Stored Procedures into a file and save on a flash disk

Is there any way to copy all the Database - Tables,Stored Procedures
into a file and save on a flash disk?
I want to be able to open it from anywhere.
Also is there anyway that I can pass the permission while I do so?
If you have mySql database with name "mydatabase" to backup you have to execute following code from a command line:
mysqldump -u root -p mydatabase > mydatabase.sql
and after that put your password
To restore it on another server, copy mydatabase.sql and execute from the command line:
mysql -u root -p mydatabase < mydatabase.sql

How to convert .sql file to tables in mysql db

I have .sql files that I'm guessing is auto-generated. I did not generate this file, but anyway it includes the database name and all tables with fields. I'd like to add the db and tables to my mysql localhost and would like to know how to do this. I have tried uploading the file but keep getting errors about my sql syntax. The syntax looks all correct to me so perhaps the .sql file needs to be changed?
From command-line:
mysql -u root -p databaseName < file.sql
Where databaseName is an already created empty database and file.sql is the .sql file you have, you must be in the same folder as the file when you run the command. This also assumes using root as the user and that it is password protected. Modify as needed for your own setup.
Additionally, you can do the reverse by flipping the angled bracket to create a database dump to a file. Like below.
mysql -u root -p databaseName > file.sql
To IMPORT:
ype the following command to import sql data file:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
In this example, import 'data.sql' file into 'blog' database using vivek as username:
$ mysql -u sat -p -h localhost blog < data.sql
If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
OR use hostname such as mysql.cyberciti.biz
$ mysql -u username -p -h mysql.cyberciti.biz database-name < data.sql
If you do not know the database name or database name is included in sql dump you can try out something as follows:
$ mysql -u username -p -h 202.54.1.10 < data.sql
Refer: http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html
If you want a GUI tool then you could probably use SQLyog or navicat for this.
You also can use a GUI tool - dbForge Studio for MySQL to execute the script. The SQL syntax check option will highlight syntax errors automatically, on file opening.

How can I import a database with MySQL from terminal?

How can I import a database with mysql from terminal?
I cannot find the exact syntax.
Assuming you're on a Linux or Windows console:
Prompt for password:
mysql -u <username> -p <databasename> < <filename.sql>
Enter password directly (not secure):
mysql -u <username> -p<PlainPassword> <databasename> < <filename.sql>
Example:
mysql -u root -p wp_users < wp_users.sql
mysql -u root -pPassword123 wp_users < wp_users.sql
See also:
4.5.1.5. Executing SQL Statements from a Text File
Note: If you are on windows then you will have to cd (change directory) to your MySQL/bin directory inside the CMD before executing the command.
Preferable way for windows:
Open the console and start the interactive MySQL mode
use <name_of_your_database>;
source <path_of_your_.sql>
mysql -u <USERNAME> -p <DB NAME> < <dump file path>
-u - for Username
-p - to prompt the Password
Eg. mysql -u root -p mydb < /home/db_backup.sql
You can also provide password preceded by -p but for the security reasons it is not suggestible. The password will appear on the command itself rather masked.
Directly from var/www/html
mysql -u username -p database_name < /path/to/file.sql
From within mysql:
mysql> use db_name;
mysql> source backup-file.sql
Open Terminal Then
mysql -u root -p
eg:- mysql -u shabeer -p
After That Create a Database
mysql> create database "Name";
eg:- create database INVESTOR;
Then Select That New Database "INVESTOR"
mysql> USE INVESTOR;
Select the path of sql file from machine
mysql> source /home/shabeer/Desktop/new_file.sql;
Then press enter and wait for some times if it's all executed then
mysql> exit
From Terminal:
mysql -uroot -p --default-character-set=utf8 database_name </database_path/database.sql
in the terminal type
mysql -uroot -p1234; use databasename; source /path/filename.sql
Below command is working on ubuntu 16.04, I am not sure it is working or not other Linux platforms.
Export SQL file:
$ mysqldump -u [user_name] -p [database_name] > [database_name.sql]
Example : mysqldump -u root -p max_development > max_development.sql
Import SQL file:
$ mysqldump -u [user_name] -p [database_name] < [file_name.sql]
Example: mysqldump -u root -p max_production < max_development.sql
Note SQL file should exist same directory
I usually use this command to load my SQL data when divided in files with names : 000-tableA.sql, 001-tableB.sql, 002-tableC.sql.
for anyvar in *.sql; do <path to your bin>/mysql -u<username> -p<password> <database name> < $anyvar; done
Works well on OSX shell.
Explanation:
First create a database or use an existing database. In my case, I am using an existing database
Load the database by giving <name of database> = ClassicModels in my case and using the operator < give the path to the database = sakila-data.sql
By running show tables, I get the list of tables as you can see.
Note : In my case I got an error 1062, because I am trying to load the same thing again.
mysql -u username -ppassword dbname < /path/file-name.sql
example
mysql -u root -proot product < /home/myPC/Downloads/tbl_product.sql
Use this from terminal
After struggling for sometime I found the information in https://tommcfarlin.com/importing-a-large-database/
Connect to Mysql (let's use root for both username and password):
mysql -uroot -proot
Connect to the database (let's say it is called emptyDatabase (your should get a confirmation message):
connect emptyDatabase
3 Import the source code, lets say the file is called mySource.sql and it is in a folder called mySoureDb under the profile of a user called myUser:
source /Users/myUser/mySourceDB/mySource.sql
Open the MySQL Command Line Client and type in your password
Change to the database you want to use for importing the .sql file data into. Do this by typing:
USE your_database_name
Now locate the .sql file you want to execute.
If the file is located in the main local C: drive directory and the .sql script file name is currentSqlTable.sql, you would type the following:
\. C:\currentSqlTable.sql
and press Enter to execute the SQL script file.
If you are using sakila-db from mysql website,
It's very easy on the Linux platform just follow the below-mentioned steps, After downloading the zip file of sakila-db, extract it. Now you will have two files, one is sakila-schema.sql and the other one is sakila-data.sql.
Open terminal
Enter command mysql -u root -p < sakila-schema.sql
Enter command mysql -u root -p < sakila-data.sql
Now enter command mysql -u root -p and enter your password, now you have entered into mysql system with default database.
To use sakila database, use this command use sakila;
To see tables in sakila-db, use show tables command
Please take care that extracted files are present in home directory.
First connect to mysql via command line
mysql -u root -p
Enter MySQL PW
Select target DB name
use <db_name>
Select your db file for import
SET autocommit=0; source /root/<db_file>;
commit;
This should do it. (thanks for clearing)
This will work even 10GB DB can be imported successfully this way. :)
In Ubuntu, from MySQL monitor, you have already used this syntax:
mysql> use <dbname>
-> The USE statement tells MySQL to use dbname as the default database for subsequent statements
mysql> source <file-path>
for example:
mysql> use phonebook;
mysql> source /tmp/phonebook.sql;
Important: make sure the sql file is in a directory that mysql can access to like /tmp
If you want to import a database from a SQL dump which might have "use" statements in it, I recommend to use the "-o" option as a safeguard to not accidentially import to a wrong database.
• --one-database, -o
Ignore statements except those those that occur while the default
database is the one named on the command line. This filtering is
limited, and based only on USE statements. This is useful for
skipping updates to other databases in the binary log.
Full command:
mysql -u <username> -p -o <databasename> < <filename.sql>
For Ubuntu/Linux users,
Extract the SQL file and paste it somewhere
e.g you pasted on desktop
open the terminal
go to your database and create a database name
Create database db_name;
Exit Mysql from your terminal
cd DESKTOP
mysql -u root -p db_name < /cd/to/mysql.sql
Enter the password:....
Before running the commands on the terminal you have to make sure that you have MySQL installed on your terminal.
You can use the following command to install it:
sudo apt-get update
sudo apt-get install mysql-server
Refrence here.
After that you can use the following commands to import a database:
mysql -u <username> -p <databasename> < <filename.sql>
The simplest way to import a database in your MYSQL from the terminal is done by the below-mentioned process -
mysql -u root -p root database_name < path to your .sql file
What I'm doing above is:
Entering to mysql with my username and password (here it is root & root)
After entering the password I'm giving the name of database where I want to import my .sql file. Please make sure the database already exists in your MYSQL
The database name is followed by < and then path to your .sql file. For example, if my file is stored in Desktop, the path will be /home/Desktop/db.sql
That's it. Once you've done all this, press enter and wait for your .sql file to get uploaded to the respective database
There has to be no space between -p and password
mysql -u [dbusername] -p[dbpassword] [databasename] < /home/serverusername/public_html/restore_db/database_file.sql
I always use it, it works perfectly. Thanks to ask this question. Have a great day. Njoy :)