Import and insert sql.gz file into database with putty

后端 未结 8 851
再見小時候
再見小時候 2021-01-30 00:12

I want to insert a sql.gz file into my database with SSH. What should I do?

For example I have a database from telephone numbers that name is numbers.

相关标签:
8条回答
  • 2021-01-30 00:43

    For an oneliner, on linux or cygwin, you need to do public key authentication on the host, otherwise ssh will be asking for password.

    
    gunzip -c numbers.sql.gz | ssh user@host mysql --user=user_name --password=your_password db_name
    

    Or do port forwarding and connect to the remote mysql using a "local" connection:

    ssh -L some_port:host:local_mysql_port user@host

    then do the mysql connection on your local machine to localhost:some_port.

    The port forwarding will work from putty too, with the similar -L option or you can configure it from the settings panel, somewhere down on the tree.

    0 讨论(0)
  • 2021-01-30 00:49

    Creating a Dump File SQL.gz on the current server

    $ sudo apt-get install pigz pv
    
    $ pv | mysqldump --user=<yourdbuser> --password=<yourdbpassword> <currentexistingdbname> --single-transaction --routines --triggers --events --quick --opt -Q --flush-logs --allow-keywords --hex-blob --order-by-primary --skip-comments --skip-disable-keys --skip-add-locks --extended-insert --log-error=/var/log/mysql/<dbname>_backup.log | pigz > /path/to/folder/<dbname>_`date +\%Y\%m\%d_\%H\%M`.sql.gz
    

    Optional: Command Arguments for connection

    --host=127.0.0.1 / localhost / IP Address of the Dump Server
    --port=3306
    

    Importing the dumpfile created above to a different Server

    $ sudo apt-get install pigz pv
    
    $ zcat /path/to/folder/<dbname>_`date +\%Y\%m\%d_\%H\%M`.sql.gz | pv | mysql --user=<yourdbuser> --password=<yourdbpassword> --database=<yournewdatabasename> --compress --reconnect --unbuffered --net_buffer_length=1048576 --max_allowed_packet=1073741824 --connect_timeout=36000 --line-numbers --wait --init-command="SET GLOBAL net_buffer_length=1048576;SET GLOBAL max_allowed_packet=1073741824;SET FOREIGN_KEY_CHECKS=0;SET UNIQUE_CHECKS = 0;SET AUTOCOMMIT = 1;FLUSH NO_WRITE_TO_BINLOG QUERY CACHE, STATUS, SLOW LOGS, GENERAL LOGS, ERROR LOGS, ENGINE LOGS, BINARY LOGS, LOGS;"
    

    Optional: Command Arguments for connection

    --host=127.0.0.1 / localhost / IP Address of the Import Server
    --port=3306
    

    mysql: [Warning] Using a password on the command line interface can be insecure. 1.0GiB 00:06:51 [8.05MiB/s] [<=> ]

    The optional software packages are helpful to import your database SQL file faster

    • with a progress view (pv)
    • Parallel gzip (pigz/unpigz) to gzip/gunzip files in parallel

    for faster zipping of the output

    0 讨论(0)
提交回复
热议问题