How can I import a database with MySQL from terminal?

后端 未结 18 1283
盖世英雄少女心
盖世英雄少女心 2020-11-27 09:14

How can I import a database with mysql from terminal?

I cannot find the exact syntax.

相关标签:
18条回答
  • 2020-11-27 09:37

    After struggling for sometime I found the information in https://tommcfarlin.com/importing-a-large-database/

    1. Connect to Mysql (let's use root for both username and password):

      mysql -uroot -proot
      
    2. 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
    
    0 讨论(0)
  • 2020-11-27 09:37

    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

    0 讨论(0)
  • 2020-11-27 09:38

    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.

    0 讨论(0)
  • 2020-11-27 09:38
    1. Open the MySQL Command Line Client and type in your password

    2. Change to the database you want to use for importing the .sql file data into. Do this by typing:

      USE your_database_name
      
    3. 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.

    0 讨论(0)
  • 2020-11-27 09:38

    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 :)

    0 讨论(0)
  • 2020-11-27 09:40

    From Terminal:

    mysql -uroot -p --default-character-set=utf8 database_name </database_path/database.sql
    
    0 讨论(0)
提交回复
热议问题