简体   繁体   中英

How to turn off autocommit in mariadb?

I am using mariasql and the mysql workbench IDE for mariasql, but I am not able to disable the autocommit mode. How do I turn off the autocommit?

In the SQL editor there is a toolbar button that allows to toggle auto commit mode:

在此处输入图片说明

Switching off auto commit will enable the 2 other blue buttons that can be used to start and commit a transaction.

I use XAMPP-VM with MariaDB with InnoDB. Couldn't manage to disable autocommit with my.conf, however, I edited mysql.server file (for XAMPP-VM it's in /opt/lampp/bin folder) and added this parameter to startup

--autocommit=0

So at the end, it looks like this

...
case "$mode" in
  'start')
    # Start daemon
    ...
      $bindir/mysqld_safe --autocommit=0 --datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &
    ...

and it did the trick. You can check it with this after stop-start (restart won't apply the change as that goes on a different path in the switch)

select *
from information_schema.global_variables
where variable_name = 'AUTOCOMMIT';

The other side of the coin is if you upgrade then you need to repeat this.

Hope, you can adopt this solution / gives you some idea how to do it!

You can set autocommit to false for the session :

SET autocommit = 0;

https://mariadb.com/kb/en/mariadb/server-system-variables/#autocommit

I consider it a mistake to turn off autocommit .

There are 3 ways for dealing with autocommit:

  • autocommit=1 -- all statements are immediately committed. This is usually fine for casual usage.

  • BEGIN ... COMMIT -- where you explicitly bracket a set of SQL statements. By having the BEGIN , you are reminded that a COMMIT will be needed.

  • autocommit=0 ... COMMIT -- I see this as error prone. You default autocommit to 0, but then forget to issue COMMIT . Then you wonder why your INSERTs disappeared.

The only way I found is to add --autocommit=0 to the startup command of the daemon.

Environment:

$ mariadb --version
mariadb  Ver 15.1 Distrib 10.5.6-MariaDB, for Linux (x86_64) using readline 5.1

$ grep PRETTY_NAME /etc/os-release 
PRETTY_NAME="Oracle Linux Server 7.9"

One-line command:

NOTE: You still need to add --autocommit=0 manually.

$ vim /usr/lib/systemd/system/mariadb.service && systemctl daemon-reload && systemctl restart mariadb && mariadb -u root -e "show variables like '%autocommit%'"

Step-by-step commands:

  1. Open the job file with a text editor
$ vim /usr/lib/systemd/system/mariadb.service
  1. Add --autocommit=0 to ExecStart (for me it was in #92)
ExecStart=/usr/sbin/mariadbd --autocommit=0 $MYSQLD_OPTS $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION
  1. Reload the daemon
$ systemctl daemon-reload
  1. Restart the service
$ systemctl restart mariadb
  1. Check the status of autocommit variable
$ mariadb -u root -e "show variables like '%autocommit%'"
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| autocommit             | OFF   |
...
+------------------------+-------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM