Skip to content

Instantly share code, notes, and snippets.

@xianx
Last active March 3, 2021 21:21
Show Gist options
  • Select an option

  • Save xianx/6828501 to your computer and use it in GitHub Desktop.

Select an option

Save xianx/6828501 to your computer and use it in GitHub Desktop.

Revisions

  1. xianx revised this gist Mar 27, 2020. 1 changed file with 19 additions and 1 deletion.
    20 changes: 19 additions & 1 deletion Limiting the number of records.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,22 @@ Limiting the number of records from mysqldump

    mysqldump --opt --where="1 limit 1000000" database

    that would give you the first million rows from every table.
    that would give you the first million rows from every table.

    ---

    As the default order is ASC which is rarely what you want in this situation, you need to have a proper database design to make DESC work out of the box. If all your tables have ONE primary key column with the same name (natural or surrogate) you can easily dump the n latest records using:

    mysqldump --opt --where="1 ORDER BY id DESC limit 1000000" --all-databases > dump.sql
    This is a perfect reason to why you should always name your PK's id and avoid composite PK's, even in association tables (use surrogate keys instead).

    ---

    f you want to get n records from a specific table you can do something like this:

    mysqldump --opt --where="1 limit 1000000" database table > dump.sql
    This will dump the first 1000000 rows from the table named table into the file dump.sql.

    ---

    Source : https://techtun.es/hzhzLZ
  2. xianx renamed this gist Mar 27, 2020. 1 changed file with 0 additions and 0 deletions.
  3. xianx revised this gist Mar 27, 2020. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions Limiting the number of records
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    Limiting the number of records from mysqldump

    mysqldump --opt --where="1 limit 1000000" database

    that would give you the first million rows from every table.
  4. xianx revised this gist Nov 1, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MySQL Backup Restore.md
    Original file line number Diff line number Diff line change
    @@ -30,5 +30,5 @@ mysql -u username -p -v Database_Name < /path/to/mysql_DUMP_File_Name.sql
    **Restoring into another Remote Host database**

    ```
    mysql --host=Host_Name --user=User_Name --password=Password --verbose Database_Name > /path/to/mysql_DUMP_File.sql
    mysql --host=Host_Name --user=User_Name --password=Password --verbose Database_Name < /path/to/mysql_DUMP_File.sql
    ```
  5. xianx revised this gist Nov 1, 2018. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion MySQL Backup Restore.md
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,10 @@ mysqldump --user=User_Name --password=Password Database_Name Table_Name > /path/
    ```
    mysql -u username -p -v Database_Name < /path/to/mysql_DUMP_File_Name.sql
    ```


    **Restoring into another Remote Host database**

    ```
    mysql --user=User_Name --password=Password --verbose Database_Name > /path/to/mysql_DUMP_File.sql
    mysql --host=Host_Name --user=User_Name --password=Password --verbose Database_Name > /path/to/mysql_DUMP_File.sql
    ```
  6. xianx revised this gist Nov 1, 2018. 2 changed files with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MySQL Backup Restore.md
    Original file line number Diff line number Diff line change
    @@ -23,8 +23,8 @@ mysqldump --user=User_Name --password=Password Database_Name Table_Name > /path/
    **Restoring the single table into another database**

    ```
    mysql -u username -p Database_Name < /path/to/mysql_DUMP_File_Name.sql
    mysql -u username -p -v Database_Name < /path/to/mysql_DUMP_File_Name.sql
    ```
    ```
    mysql --user=User_Name --password=Password Database_Name > /path/to/mysql_DUMP_File.sql
    mysql --user=User_Name --password=Password --verbose Database_Name > /path/to/mysql_DUMP_File.sql
    ```
  7. xianx revised this gist Sep 20, 2017. No changes.
  8. xianx revised this gist Sep 20, 2017. No changes.
  9. xianx revised this gist Apr 21, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Basic MySQL Backup.md
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,5 @@ mysqldump -u USERNAME -pPASSWORD -v DATABASENAME > DATABASENAME.sql
    ** Restore single database with verbose output**

    ```
    mysql -u USERNAME -pPASSWORD -v DATABASENAME < DATABASENAME.sql
    mysql -u USERNAME -pPASSWORD -v DATABASENAME < DATABASENAME.sql
    ```
  10. xianx revised this gist Apr 21, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Basic MySQL Backup.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@
    ** Backup a single database with verbose output
    ** Backup a single database with verbose output**

    ```
    mysqldump -u USERNAME -pPASSWORD -v DATABASENAME > DATABASENAME.sql
    ```

    ** Restore single database with verbose output
    ** Restore single database with verbose output**

    ```
    mysql -u USERNAME -pPASSWORD -v DATABASENAME < DATABASENAME.sql
  11. xianx revised this gist Apr 21, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Basic MySQL Backup.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@
    *** Backup a single database with verbose output
    ** Backup a single database with verbose output

    ```
    mysqldump -u USERNAME -pPASSWORD -v DATABASENAME > DATABASENAME.sql
    ```

    *** Restore single database with verbose output
    ** Restore single database with verbose output

    ```
    mysql -u USERNAME -pPASSWORD -v DATABASENAME < DATABASENAME.sql
  12. xianx revised this gist Apr 21, 2015. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions Basic MySQL Backup.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    ### Backup a single database
    *** Backup a single database with verbose output

    ```
    mysqldump -u USERNAME -pPASSWORD -v DATABASENAME > DATABASENAME.sql
    ```
    ```

    *** Restore single database with verbose output

    ```
    mysql -u USERNAME -pPASSWORD -v DATABASENAME < DATABASENAME.sql
  13. xianx revised this gist Apr 21, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Basic MySQL Backup.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    ### Backup a single database

    ```
    mqsqldump -u USERNAME -pPASSWORD -v DATABASENAME > DATABASENAME.sql
    mysqldump -u USERNAME -pPASSWORD -v DATABASENAME > DATABASENAME.sql
    ```
  14. xianx revised this gist Apr 21, 2015. 2 changed files with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions Basic MySQL Backup.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    ### Backup a single database

    ```
    mqsqldump -u USERNAME -pPASSWORD -v DATABASENAME > DATABASENAME.sql
    ```
    File renamed without changes.
  15. xianx revised this gist Oct 4, 2013. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions new_gist_file.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@

    ```
    mysqldump -u username -p database --ignore-table=database.table1 --ignore-table=database.table2 > database.sql
    ```
    ```
    mysqldump -u dave -ppassword -h localhost --ignore-table=my_db_name.my_table_name my_db_name
    ```
  16. xianx revised this gist Oct 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MySQL Backup Restore.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ mysqldump --user=User_Name --password=Password --all-databases > /path/to/mysql_
    ### Single Table Backup and Restore


    **Backup a single table from a database**
    **Backingup a single table from a database**

    ```
    mysqldump -u username -p Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
  17. xianx revised this gist Oct 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MySQL Backup Restore.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ mysqldump --user=User_Name --password=Password --all-databases > /path/to/mysql_
    ### Single Table Backup and Restore


    **Backuping a single table from a database**
    **Backup a single table from a database**

    ```
    mysqldump -u username -p Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
  18. xianx revised this gist Oct 4, 2013. 2 changed files with 16 additions and 22 deletions.
    36 changes: 15 additions & 21 deletions MySQL Backup Restore → MySQL Backup Restore.md
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,30 @@
    **************************************************
    *
    * All Database Backup and Restore
    *
    **************************************************

    ### All Database Backup and Restore

    **Backingup all database**

    #Backingup all database

    ```
    mysqldump --user=User_Name --password=Password --all-databases > /path/to/mysql_DUMP_File.sql
    ```



    **************************************************
    *
    * Single Table Backup and Restore
    *
    **************************************************
    ### Single Table Backup and Restore


    **Backuping a single table from a database**

    #Backuping a single table from a database

    ```
    mysqldump -u username -p Database_Name Table_Name > /path/to/mysql_DUMP_File.sql

    ```
    ```
    mysqldump --user=User_Name --password=Password Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    **Restoring the single table into another database**


    #Restoring the single table into another database

    ```
    mysql -u username -p Database_Name < /path/to/mysql_DUMP_File_Name.sql

    ```
    ```
    mysql --user=User_Name --password=Password Database_Name > /path/to/mysql_DUMP_File.sql

    ```
    2 changes: 1 addition & 1 deletion MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    mysqldump -u username -p --where="(%OPTION%)" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    **where %OPTION%**
    __where %OPTION%__

    ```
    --where="TRUE ORDER BY id DESC LIMIT 1000"
  19. xianx revised this gist Oct 4, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@

    ## Latest 'X' Records from a Single Table
    ### Latest 'X' Records from a Single Table


    ```
    @@ -16,7 +16,7 @@ mysqldump -u username -p --where="(%OPTION%)" Database_Name Table_Name > /path/
    ```


    ## Latest 'X' Records from a All Database
    ### Latest 'X' Records from a All Database

    ```
    mysqldump -u User_Name -p --where="true limit 1000" --all-databases > /path/to/mysql_DUMP_File.sql
  20. xianx revised this gist Oct 4, 2013. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@

    Latest 'X' Records from a Single Table
    ----
    ## Latest 'X' Records from a Single Table


    ```
    mysqldump -u username -p --where="(%OPTION%)" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    @@ -16,8 +16,7 @@ mysqldump -u username -p --where="(%OPTION%)" Database_Name Table_Name > /path/
    ```


    Latest 'X' Records from a All Database
    ----
    ## Latest 'X' Records from a All Database

    ```
    mysqldump -u User_Name -p --where="true limit 1000" --all-databases > /path/to/mysql_DUMP_File.sql
  21. xianx revised this gist Oct 4, 2013. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -9,22 +9,21 @@ mysqldump -u username -p --where="(%OPTION%)" Database_Name Table_Name > /path/
    **where %OPTION%**

    ```
    --where="TRUE ORDER BY id DESC LIMIT 1000"
    --where="TRUE LIMIT 500"
    --where="mailbox_id=45"
    --where="mailbox_id=45 AND TRUE ORDER BY id DESC LIMIT 300"
    ```


    Latest 'X' Records from a Single Table
    Latest 'X' Records from a All Database
    ----


    ```
    mysqldump -u User_Name -p --where="true limit 1000" --all-databases > /path/to/mysql_DUMP_File.sql
    ```




    --opt => Shorthand for --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset
  22. xianx revised this gist Oct 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Latest 'X' Records from a Single Table
    mysqldump -u username -p --where="(%OPTION%)" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    where %OPTION%
    **where %OPTION%**

    ```
    --where="TRUE LIMIT 500"
  23. xianx revised this gist Oct 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Latest 'X' Records from a Single Table
    mysqldump -u username -p --where="(%OPTION%)" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    __ --where %OPTION% __
    where %OPTION%

    ```
    --where="TRUE LIMIT 500"
  24. xianx revised this gist Oct 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Latest 'X' Records from a Single Table
    mysqldump -u username -p --where="(%OPTION%)" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    **--where %OPTION%**
    __ --where %OPTION% __

    ```
    --where="TRUE LIMIT 500"
  25. xianx revised this gist Oct 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Latest 'X' Records from a Single Table
    mysqldump -u username -p --where="(%OPTION%)" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    *--where %OPTION%*
    **--where %OPTION%**

    ```
    --where="TRUE LIMIT 500"
  26. xianx revised this gist Oct 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Latest 'X' Records from a Single Table
    mysqldump -u username -p --where="(%OPTION%)" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    #### --where %OPTION%
    *--where %OPTION%*

    ```
    --where="TRUE LIMIT 500"
  27. xianx revised this gist Oct 4, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,10 @@ Latest 'X' Records from a Single Table
    ----

    ```
    mysqldump -u username -p --where="(OPTION)" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    mysqldump -u username -p --where="(%OPTION%)" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    #### --where
    #### --where %OPTION%

    ```
    --where="TRUE LIMIT 500"
  28. xianx revised this gist Oct 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ Latest 'X' Records from a Single Table
    ----

    ```
    mysqldump -u username -p --where="true LIMIT 1000" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    mysqldump -u username -p --where="(OPTION)" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    #### --where
  29. xianx revised this gist Oct 4, 2013. 1 changed file with 6 additions and 18 deletions.
    24 changes: 6 additions & 18 deletions MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -6,29 +6,17 @@ Latest 'X' Records from a Single Table
    mysqldump -u username -p --where="true LIMIT 1000" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    ### --where
    #### --where

    ```
    mysqldump --where="TRUE LIMIT 500"
    mysqldump --where="mailbox_id=45"
    mysqldump --where="mailbox_id=45 AND TRUE ORDER BY id DESC LIMIT 300"
    --where="TRUE LIMIT 500"
    --where="mailbox_id=45"
    --where="mailbox_id=45 AND TRUE ORDER BY id DESC LIMIT 300"
    ```
    ```
    mysqldump -u username -p --where="id<1000" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    ```
    mysqldump --user=User_Name --password=Password --opt --where="true LIMIT 1000" > /path/to/mysql_DUMP_File.sql
    ```

    ```
    mysqldump -u username -p --where="TRUE ORDER BY id DESC LIMIT 1000" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```




    ## Latest 'X' Records from a Single Table
    Latest 'X' Records from a Single Table
    ----


    ```
  30. xianx revised this gist Oct 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MySQL Backup of Latest 'X' Records.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Latest 'X' Records from a Single Table
    mysqldump -u username -p --where="true LIMIT 1000" Database_Name Table_Name > /path/to/mysql_DUMP_File.sql
    ```

    ###
    ### --where

    ```
    mysqldump --where="TRUE LIMIT 500"