Here, someone asks the same question on mysql forums, but there is no really authoritative answer. They land on 'To be safe, first lock the table. Then you can count on the identifiers to be consistent.'
My advice is this:
If you insert a lot of rows and do not mind blocking other threads, lock the table, do inserts, and then open. Since the table has been locked, you can rely on the inserted identifiers to be sequential (or regardless of your auto-increment setting).
If you are doing a small number of inserts or do not want to block other threads, just do them one at a time and call last_insert_id () after each.
source share