1

I'm using Ubuntu 16.04 and I'm able to use MySql from the command line fine. I can create a db, add tables, add rows to tables, get queries etc.

What I can't do is write an sql file and run it.

From a regular terminal I change into the directory containing my script test.sql

Then I run mysql -u root -p > test.mysql and it asks me for my password. If I put the wrong password I get an error and if I put the correct password nothing happens, the terminal behaves as if its expecting further input.

If I have MySql started and I do

mysql>SOURCE /home/ryan/MYSQL_Scripts/test.sql

I get Failed to open file '/home/ryan/MYSQL Scripts/test.sql', error: 2

I know this means the file could not be found or supposedly does not exist yet from my explorer I right clicked on the file to get info on its path so I think the path is okay.

I don't think there's anything wrong with my script since I can run the commands individually from the mysql prompt. Here's my script:

USE first_db;

CREATE TABLE Department(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    name VARCHAR(30) NOT NULL);

CREATE TABLE Employees(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    name VARCHAR(30) NOT NULL,
    department_id INT(6) NOT NULL);

INSERT INTO Department (name) VALUES ("Fire");

SELECT * FROM Department; 

I of course have a db, first_db, already created and am able to interact w/ it from the mysql prompt I just need help using mysql scripts.

Some advice would be much appreciated. Thanks!

`

2
  • You have a problem with your path. Find out why and you solve your problem. Commented Jul 5, 2018 at 20:28
  • 2
    Flip your >: mysql -u root -p < test.mysql Otherwise you are redirecting your stdout from mysql to that file (and blowing away it's content when you do so). Commented Jul 5, 2018 at 20:29

1 Answer 1

3

JNevill's method worked right away, mysql -u root -p < test1.sql is the correct command to execute a mysql script from the command line.

Robert Harvey is onto something w/ the path reference. Not sure yet why the path I've used is incorrect though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.