Importing a SQL db into mysql using the terminal

34,888

Solution 1

I found an SO post here.

I used "source" like so:

SOURCE /Users/myname/Desktop/somedb.sql;

That worked. Great but the internet seemed to want me to use the method like so:

mysql -u username -p password databasename < filename.sql

I may post another question on when to use that second method but in the meantime I just used source from a SQL dump file

Solution 2

Using MAMP Pro, created "uploads" dir in MAMP and put my SQL file in there called "file.sql". Ran the query below in terminal and worked for me.

Make sure to replace brackets and and user info with no spaces after "-u" or "-p"

/Applications/MAMP/Library/bin/mysql -u<username> -p<root> <db_name> < /Applications/MAMP/uploads/file.sql

Solution 3

You can try this way..

Go to the mysql prompt and then type the following.

mysql> \. <path> /filename.sql

Note the gap between . and the path of the sql file.Hope this works.

Solution 4

Type this on terminal

sudo mysql -p database_name < folder/database_dump_file.sql

it will then ask you for the mysql password

Share:
34,888
Doug Fir
Author by

Doug Fir

Updated on June 25, 2020

Comments

  • Doug Fir
    Doug Fir about 4 years

    I'm struggling to do this.

    I have created a new database in the terminal called "somedb" using

    CREATE DATABASE somedb
    

    On my desktop I have the SQL dump downloaded from phpMyadmin: somedb.sql

    I have tried:

    somedb < /Users/myname/Desktop/somedb.sql
    

    Result: ERROR 1064 (42000): You have an error in your SQL syntax

    mysql -u myname -p -h localhost somedb </Users/myname/Desktop/somedb.sql
    

    Result: ERROR 1064 (42000): You have an error in your SQL syntax;

    I'm new to SQL (The purpose of importing this db is for a text book exercise)

    I have granted myself all privileges and there is no password.

    Any idea what I'm doing wrong?

    Here is the top of the SQL dump file:

    -- phpMyAdmin SQL Dump
    -- version 4.0.2
    -- http://www.phpmyadmin.net
    --
    -- Host: localhost
    -- Generation Time: Jun 18, 2013 at 02:22 PM
    -- Server version: 5.5.31-30.3
    -- PHP Version: 5.2.17
    
    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";
    
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
    
    --
    -- Database: `somedb`
    --
    CREATE DATABASE IF NOT EXISTS `somedb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
    USE `somedb`;
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `actions`
    --
    
    CREATE TABLE IF NOT EXISTS `actions` (
      `action_id` int(11) NOT NULL AUTO_INCREMENT,
      `action` varchar(75) NOT NULL,
      `qualifiers` text NOT NULL,
      `response` varchar(75) NOT NULL,
      `response_vars` text NOT NULL,
      `active` tinyint(4) NOT NULL,
      PRIMARY KEY (`action_id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='Stores user defined actions triggered by certain events' AUTO_INCREMENT=3 ;
    
    -- --------------------------------------------------------
    
    --