如何关闭 MySQL 客户端的自动提交?

发布于 2024-08-22 09:31:32 字数 167 浏览 12 评论 0 原文

我有一个网络应用程序,它是在假设数据库上打开自动提交的情况下编写的,因此我不想在那里进行任何更改。然而,我能找到的所有文档似乎都只讨论在数据库上使用 init_connect ,即所有客户端连接的全局设置。

有没有办法在Linux命令行上运行mysql时设置autocommit=0(而不必每次都输入)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

烛影斜 2024-08-29 09:31:33

看起来您可以将其添加到 ~/.my.cnf 中,但需要将其作为参数添加到 [client] 部分中的 init-command 标志,如下所示:

[client]
init-command='set autocommit=0'

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:

[client]
init-command='set autocommit=0'
π浅易 2024-08-29 09:31:33

这对于检查自动提交的状态很有用;

select @@autocommit;

This is useful to check the status of autocommit;

select @@autocommit;
合久必婚 2024-08-29 09:31:33

你是说mysql文本控制台吗?然后:

START TRANSACTION;
  ...
  your queries.
  ...
COMMIT;

这是我推荐的。

但是,如果您想避免每次需要运行此类查询时都键入此内容,请将以下内容添加到 my.cnf 文件的 [mysqld] 部分。

init_connect='set autocommit=0'

不过,这会将每个客户端的自动提交设置为关闭。

Do you mean the mysql text console? Then:

START TRANSACTION;
  ...
  your queries.
  ...
COMMIT;

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.

init_connect='set autocommit=0'

This would set autocommit to be off for every client though.

拥有 2024-08-29 09:31:33

对于自动提交关闭,请务必使用以下命令。在 my.cnf 文件中设置如下:

    [mysqld]
    autocommit=0

For auto commit off then use the below command for sure. Set below in my.cnf file:

    [mysqld]
    autocommit=0
暮倦 2024-08-29 09:31:33

您无需在恢复时手动关闭自动提交,您就可以将 MySQL 数据转储为将所有必要的语句直接包含到 SQL 文件中。

mysqldump 的命令行参数是--no-autocommit。您还可以考虑添加 --opt 来设置其他参数的组合以加快恢复操作。

下面是我使用的完整 mysqldump 命令行示例,其中包含 --no-autocommit--opt

mysqldump -hlocalhost -uMyUser -p'MyPassword' --no-autocommit --opt --default-character-set=utf8 --quote-names  MyDbName  >  dump.sql

有关这些参数的详细信息,请参阅 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:

mysqldump -hlocalhost -uMyUser -p'MyPassword' --no-autocommit --opt --default-character-set=utf8 --quote-names  MyDbName  >  dump.sql

For details of these parameters see the reference of mysqldump

苦妄 2024-08-29 09:31:33

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;

源来凯始玺欢你 2024-08-29 09:31:32

也许最好的方法是编写一个脚本来启动 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 提供的

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

Expect 脚本非常强大,可以让生活变得更加轻松,就像在本例中一样。

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

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

#! /usr/bin/expect

然后更改权限你的脚本的...

chmod 0744 myscript

然后调用脚本

./myscript

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.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

紧拥背影 2024-08-29 09:31:32

您可以通过 3 种不同的方式执行此操作:

  1. 在执行 INSERT 之前,始终发出 BEGIN; 语句。这将关闭自动提交。一旦您希望将数据保留在数据库中,您将需要执行 COMMIT;

  2. 每次实例化数据库连接时,请使用 autocommit=0;

  3. 对于全局设置,请在 MySQL 的 my.cnf 配置文件中添加 autocommit=0 变量。

You do this in 3 different ways:

  1. Before you do an INSERT, always issue a BEGIN; statement. This will turn off autocommits. You will need to do a COMMIT; once you want your data to be persisted in the database.

  2. Use autocommit=0; every time you instantiate a database connection.

  3. For a global setting, add a autocommit=0 variable in your my.cnf configuration file in MySQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文