如何关闭MySQL客户端的自动提交? [英] How do I turn off autocommit for a MySQL client?

查看:696
本文介绍了如何关闭MySQL客户端的自动提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,该应用程序是在假定数据库已打开自动提交的情况下编写的,因此我不想在此进行任何更改.但是,我只能找到的所有文档似乎都在谈论在数据库上使用init_connect,即所有客户端连接的全局设置.

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.

有没有一种方法可以仅在Linux命令行上运行mysql时设置autocommit = 0(而不必每次都键入它)?

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)?

推荐答案

也许最好的方法是编写一个脚本,该脚本启动mysql命令行客户端,然后在将控件移交给您之前自动运行所需的任何sql.

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附带了一个名为"expect"的应用程序.它以模仿您的按键的方式与外壳交互.可以将其设置为启动mysql,等待您输入密码.运行其他命令,例如SET autocommit = 0;,然后进入交互模式,以便您可以运行任何所需的命令.

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.

有关命令SET autocommit = 0;的更多信息,请参见. http://dev.mysql.com/doc/refman/5.0/en/innodb-transaction-model.html

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

在我使用ssh登录,连接到远程服务器,启动应用程序输入我的用户名和密码,然后将控制权移交给我的情况下,我使用Expect登录到命令行实用程序.节省了我很多的输入时间:)

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

Michael Hinds提供的期望脚本

Expect script provided by Michael Hinds

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

expect非常强大,在这种情况下,可以使生活变得更加轻松.

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

如果您想使脚本在不调用的情况下运行,请使用shebang行

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

将其作为脚本的第一行插入(提示:使用which expect查找所需可执行文件的位置)

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

然后调用脚本

./myscript

DC

这篇关于如何关闭MySQL客户端的自动提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆