如何关闭 MySQL 客户端的自动提交?
我有一个网络应用程序,它是在假设数据库上打开自动提交的情况下编写的,因此我不想在那里进行任何更改。然而,我能找到的所有文档似乎都只讨论在数据库上使用 init_connect ,即所有客户端连接的全局设置。
有没有办法在Linux命令行上运行mysql时设置autocommit=0(而不必每次都输入)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
看起来您可以将其添加到 ~/.my.cnf 中,但需要将其作为参数添加到 [client] 部分中的 init-command 标志,如下所示:
It looks like you can add it to your ~/.my.cnf, but it needs to be added as an argument to the init-command flag in your [client] section, like so:
这对于检查自动提交的状态很有用;
This is useful to check the status of autocommit;
你是说mysql文本控制台吗?然后:
这是我推荐的。
但是,如果您想避免每次需要运行此类查询时都键入此内容,请将以下内容添加到 my.cnf 文件的 [mysqld] 部分。
不过,这会将每个客户端的
自动提交
设置为关闭。Do you mean the mysql text console? Then:
Is what I recommend.
However if you want to avoid typing this each time you need to run this sort of query, add the following to the [mysqld] section of your my.cnf file.
This would set
autocommit
to be off for every client though.对于自动提交关闭,请务必使用以下命令。在
my.cnf
文件中设置如下:For auto commit off then use the below command for sure. Set below in
my.cnf
file:您无需在恢复时手动关闭自动提交,您就可以将 MySQL 数据转储为将所有必要的语句直接包含到 SQL 文件中。
mysqldump 的命令行参数是
--no-autocommit
。您还可以考虑添加--opt
来设置其他参数的组合以加快恢复操作。下面是我使用的完整 mysqldump 命令行示例,其中包含
--no-autocommit
和--opt
:有关这些参数的详细信息,请参阅 mysqldump的参考
Instead of switching autocommit off manually at restore time you can already dump your MySQL data in a way that includes all necessary statements right into your SQL file.
The command line parameter for mysqldump is
--no-autocommit
. You might also consider to add--opt
which sets a combination of other parameters to speed up restore operations.Here is an example for a complete mysqldump command line as I use it, containing
--no-autocommit
and--opt
:For details of these parameters see the reference of mysqldump
mysql中autocommit变量的默认值为1;
要查看 mysql 中自动提交的当前值,请使用命令
mysql> 选择@@autocommit;
那么如果你想关闭自动提交使用命令
mysql>设置自动提交 = 0;
Default value of autocommit variable in mysql is 1;
To view the current value of autocommit in mysql use command
mysql> select @@autocommit;
then if you want to off autocommit use command
mysql> set autocommit = 0;
也许最好的方法是编写一个脚本来启动 mysql 命令行客户端,然后在将控制权交给您之前自动运行您想要的任何 sql。
Linux 附带了一个名为“expect”的应用程序。它与 shell 进行交互,以模仿您的击键。可以设置启动mysql,等待你输入密码。运行更多命令,例如 SET autocommit = 0; ,然后进入交互模式,以便您可以运行任何您想要的命令。
有关命令
SET autocommit = 0;
的更多信息,请参阅.. http://dev.mysql.com/doc/refman/5.0/en/innodb-transaction-model.html在我的例子中,我使用expect登录命令行实用程序它启动 ssh,连接到远程服务器,启动应用程序,输入我的用户名和密码,然后将控制权交给我。节省了我大量的打字时间:)
http://linux.die.net/man/1/expect< /a>
DC
Michael Hinds Expect 提供的
Expect 脚本非常强大,可以让生活变得更加轻松,就像在本例中一样。
如果您想让脚本在不调用 Expect 的情况下运行,请使用 shebang 行,
将其作为脚本中的第一行插入(提示:使用
which Expect
查找您的 Expect 可执行文件的位置),然后更改权限你的脚本的...
然后调用脚本
DC
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.htmlI 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
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)then change the permissions of your script with..
then call the script
DC
您可以通过 3 种不同的方式执行此操作:
在执行
INSERT
之前,始终发出BEGIN;
语句。这将关闭自动提交。一旦您希望将数据保留在数据库中,您将需要执行COMMIT;
。每次实例化数据库连接时,请使用
autocommit=0;
。对于全局设置,请在 MySQL 的
my.cnf
配置文件中添加autocommit=0
变量。You do this in 3 different ways:
Before you do an
INSERT
, always issue aBEGIN;
statement. This will turn off autocommits. You will need to do aCOMMIT;
once you want your data to be persisted in the database.Use
autocommit=0;
every time you instantiate a database connection.For a global setting, add a
autocommit=0
variable in yourmy.cnf
configuration file in MySQL.