Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is MySQL "thread safe" from a php script?

If I have a php script which calls INSERT, UPDATE, DELETE, etc on a MySQL connection, and that script gets called at uncontrolled times by a POST operation, is it always "safe" (ie, will not result in corrupt tables or collisions during requests)?

For example, if 500 requests come during a 1-second period.

If so, how does php/mysql achieve this?

If not, what does one need to do to guarantee "serial" access or safe simultaneous access?

like image 238
SG1 Avatar asked Mar 01 '12 21:03

SG1


People also ask

Is MySQL database thread-safe?

It is not thread-safe, so call it before threads are created, or protect the call with a mutex. Arrange for mysql_thread_init() to be called early in the thread handler before calling any MySQL function. (If you call mysql_init() , it calls mysql_thread_init() for you.)

Should PHP download thread-safe?

Thread-safety is recommended when the web server run multiple threads of execution simultaneously for different requests. In Thread Safety binary can work in a multi-threaded web server context. Thread Safety works by creating a local storage copy in each thread so that the data will not collide with another thread.

Can we use PHP and MySQL together?

With PHP, you can connect to and manipulate databases. MySQL is the most popular database system used with PHP.

How do I know if PHP is thread-safe?

Open a phpinfo() and search for the line Thread safety. For a thread-safe build you should find enable. As specified in the comments by Muhammad Gelbana you can also use: On Windows : php -i|findstr "Thread"


2 Answers

MySQL uses locking (table-level for MyISAM or row-level for InnoDB), which does not allow 2 processes (2 calls to the script) to modify the same row. So the table won't crash*, but it's possible that MySQL can't handle the number of request in reasanoble time and the requests will wait. You should always optimize your queries to be as fast as possible.

*MyISAM could crash on insert/update intensive applications, but it has automatic recovery. However keep in mind that in such application, InnoDB has far better performance

like image 107
Maxim Krizhanovsky Avatar answered Oct 14 '22 12:10

Maxim Krizhanovsky


is it always "safe" (ie, will not result in corrupt tables or collisions during requests)?

yes

If so, how does php/mysql achieve this?

table/row locks.

like image 37
Your Common Sense Avatar answered Oct 14 '22 11:10

Your Common Sense