How do I import an SQL file using the command line in MySQL?

后端 未结 30 2244
不知归路
不知归路 2020-11-22 06:48

I have a .sql file with an export from phpMyAdmin. I want to import it into a different server using the command line.

I have a Windows Ser

30条回答
  •  醉话见心
    2020-11-22 07:29

    Use:

    mysql -u root -p password -D database_name << import.sql
    

    Use the MySQL help for details - mysql --help.

    I think these will be useful options in our context:

    [~]$ mysql --help
    mysql  Ver 14.14 Distrib 5.7.20, for osx10.12 (x86_64) using  EditLine wrapper
    Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
    Usage: mysql [OPTIONS] [database]
      -?, --help          Display this help and exit.
      -I, --help          Synonym for -?
      --bind-address=name IP address to bind to.
      -D, --database=name Database to use.
      --delimiter=name    Delimiter to be used.
      --default-character-set=name Set the default character set.
      -f, --force         Continue even if we get an SQL error.
      -p, --password[=name] Password to use when connecting to server.
      -h, --host=name     Connect to host.
      -P, --port=#        Port number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306).
      --protocol=name     The protocol to use for connection (tcp, socket, pipe,
      -s, --silent        Be more silent. Print results with a tab as separator, each row on new line.
      -v, --verbose       Write more. (-v -v -v gives the table output format).
      -V, --version       Output version information and exit.
      -w, --wait          Wait and retry if connection is down.
    

    What is fun, if we are importing a large database and not having a progress bar. Use Pipe Viewer and see the data transfer through the pipe

    For Mac, brew install pv

    For Debian/Ubuntu, apt-get install pv.

    For others, refer to pv - Pipe Viewer

    pv import.sql | mysql -u root -p password -D database_name
    
    1.45GiB 1:50:07 [339.0KiB/s]   [=============>      ] 14% ETA 11:09:36
    1.46GiB 1:50:14 [ 246KiB/s]     [=============>      ] 14% ETA 11:09:15
    1.47GiB 1:53:00 [ 385KiB/s]     [=============>      ] 14% ETA 11:05:36
    

提交回复
热议问题