Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run sql file in database from terminal

I have a server and within that server I have an sql script in the /var/www/ directory. That script is to create tables and databases. I need to know how to execute this sql script from terminal or some client while keeping the file on the server.

PS- I don't know much of the terminology for this topic so I will look into other suggestions for my approach.

like image 470
Brennan Casey Avatar asked Aug 04 '15 22:08

Brennan Casey


People also ask

How do I run a SQL File in terminal?

use the MySQL command line client: mysql -h hostname -u user database < path/to/test. sql. Install the MySQL GUI tools and open your SQL file, then execute it.

How do I run a .SQL File in a database?

To run SQL script in MySQL, use the MySQL workbench. First, you need to open MySQL workbench. Now, File -> Open SQL Script to open the SQL script. Note − Press OK button twice to connect with MySQL.

How do I run a MySQL script in terminal?

First in terminal you have to login using your MySQL username and password Eg: mysql -uroot -p then once loged in select your data base using use <yourdatabasename then you can run this command.


2 Answers

I presume that it is MYSQL. To run it from Unix / Linux environment you must do this:

$ mysql -h "server-name" -u "your_username" -p "your_password" "database-name" < "filename.sql" 

There is another way:

mysql --host=localhost --user=your_username --password=your_password  -e "filename.sql" 
like image 141
Tomislav Avatar answered Sep 28 '22 13:09

Tomislav


Try this:

mysql -u your_username -p your_password  use db_name  source <path_to_sql_file>/file.sql 
like image 33
Max Droid Avatar answered Sep 28 '22 14:09

Max Droid