Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I turn off autocommit for a MySQL client?

I have a web app that has been written with the assumption that autocommit is turned on on the database, so I don't want to make any changes there. However all the documentation I can find only seems to talk about using init_connect on the database, i.e. a global setting for all client connections.

Is there a way to set autocommit=0 just when running mysql on a Linux command line (without having to type it in every time)?

like image 588
Michael Hinds Avatar asked Feb 17 '10 12:02

Michael Hinds


People also ask

How do I change autocommit in MySQL?

To use multiple-statement transactions, switch autocommit off with the SQL statement SET autocommit = 0 and end each transaction with COMMIT or ROLLBACK as appropriate. To leave autocommit on, begin each transaction with START TRANSACTION and end it with COMMIT or ROLLBACK .

How do I turn off auto COMMIT mode?

You can turn off auto commit by setting implicit_transactions ON. Run below T-SQL command to turn off autocommit in SQL Server. When the setting is ON, it returns to implicit transaction mode. In implicit transaction mode, every change you make starts a transactions which you have to commit manually.

How do I know if MySQL autocommit is enabled?

To determine the current state of autocommit mode use the SQL command SELECT @@autocommit . Be aware: the mysql_rollback() function will not work if autocommit mode is switched on.

How do I turn off autocommit in MySQL workbench?

0, you can set the "Leave autocommit mode enabled by default" preference. Set it under Preferences --> SQL Queries --> General. Show activity on this post. Try SET SESSION autocommit = 0; This switch the autocommit flag to OFF in that session.


1 Answers

Perhaps the best way is to write a script that starts the mysql command line client and then automatically runs whatever sql you want before it hands over the control to you.

linux comes with an application called 'expect'. it interacts with the shell in such a way as to mimic your key strokes. it can be set to start mysql, wait for you to enter your password. run further commands such as SET autocommit = 0; then go into interactive mode so you can run any command you want.

for more on the command SET autocommit = 0; see.. http://dev.mysql.com/doc/refman/5.0/en/innodb-transaction-model.html

I use expect to log in to a command line utility in my case it starts ssh, connects to the remote server, starts the application enters my username and password then turns over control to me. saves me heaps of typing :)

http://linux.die.net/man/1/expect

DC

Expect script provided by Michael Hinds

spawn /usr/local/mysql/bin/mysql  expect "mysql>"  send "set autocommit=0;\r"  expect "mysql>" interact 

expect is pretty powerful and can make life a lot easier as in this case.

if you want to make the script run without calling expect use the shebang line

insert this as the first line in your script (hint: use which expect to find the location of your expect executable)

#! /usr/bin/expect 

then change the permissions of your script with..

chmod 0744 myscript 

then call the script

./myscript 

DC

like image 97
DeveloperChris Avatar answered Oct 09 '22 03:10

DeveloperChris