Updating AUTO_INCREMENT value of all tables in a MySQL database

前端 未结 8 1678
旧巷少年郎
旧巷少年郎 2020-12-02 22:52

It is possbile set/reset the AUTO_INCREMENT value of a MySQL table via

ALTER TABLE some_table AUTO_INCREMENT = 1000

However I need

8条回答
  •  时光说笑
    2020-12-02 23:08

    I found this gist on github and it worked like a charm for me: https://gist.github.com/abhinavlal/4571478

    The command:

    mysql -Nsr -e "SELECT t.table_name FROM INFORMATION_SCHEMA.TABLES t WHERE t.table_schema = 'DB_NAME'" | xargs -I {} mysql DB_NAME -e "ALTER TABLE {} AUTO_INCREMENT = 1;"
    

    If your DB requires a password, you unfortunately have to put that in the command for it to work. One work-around (still not great but works) is to put the password in a secure file. You can always delete the file after so the password doesn't stay in your command history:

     ... | xargs -I {} mysql -u root -p`cat /path/to/pw.txt` DB_NAME -e...
    

提交回复
热议问题