mysql中innodb数据库事务修改多记录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql中innodb数据库事务修改多记录相关的知识,希望对你有一定的参考价值。
已经 SET AUTOCOMMIT = 0
php下。如果同时两三条插入或者修改语句,commit或回滚(rollback)可以使用。
但是当有五六条以上的修改语句时,就变成自动提交了。不能做回滚操作了。这是怎么回事?
如何解决
开始事务
try
修改数据不管多少数据
.............
提交事务
except (意外)
回滚事务
end追问
你在开玩笑呢?我这说是php了。
并且问题是遇到错误,使用回滚事务,并没有回滚。
或者说,没有使用commit,事务就已经提交修改实际的数据库了。
事务是数据库支持的,跟你用什么语言开发没关系
一般mysql数据库默认的引擎是MyISAM,这种引擎不支持事务!如果要让MYSQL支持事务,可以自己手动修改
是。那不探讨用什么语言。
我好像也说的很清楚了,
我问的问题是,
如果在begin的后面有两三条sql修改语句,使用rollback可以正常回滚。如果有五六条语句,使用rollback就不能回滚了,没有使用commit,数据库就已经更改记录了。
我的标题也写了,数据库类型是:innodb
这才是我问的问题。
mysql8学习笔记30--InnoDB内核3
[root@localhost ~]# ll -h /mysql8/mysql_data/ibdata1 -rw-r-----. 1 mysql mysql 12M May 19 09:38 /mysql8/mysql_data/ibdata1 #8.0之前默认情况下回滚段存放在ibdata1中,做为系统表空间得一部分。 [root@localhost ~]# ll -h /mysql8/mysql_data/undo* -rw-r-----. 1 mysql mysql 11M May 19 09:38 /mysql8/mysql_data/undo_001#8.0之后,自己独立undo表空间。 -rw-r-----. 1 mysql mysql 11M May 19 09:38 /mysql8/mysql_data/undo_002 [root@localhost ~]#
mysql> show variables like \'%innodb_undo%\'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_undo_directory | ./ | | innodb_undo_log_encrypt | OFF | | innodb_undo_log_truncate | ON | | innodb_undo_tablespaces | 2 | +--------------------------+-------+ 4 rows in set (0.00 sec)
mysql> show variables like \'%per_table%\'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | innodb_file_per_table | ON | +-----------------------+-------+ 1 row in set (0.00 sec) mysql>
如果参数为OFF或者创建表时指定用系统表空间,则不会创建独立的表空间。
mysql> create table test1(id int,sno int) tablespace=innodb_system; Query OK, 0 rows affected (0.38 sec) mysql> create table test2(id int,sno int); Query OK, 0 rows affected (0.04 sec) [root@localhost ~]# ll -h /mysql8/mysql_data/school/test* -rw-r----- 1 mysql mysql 112K May 19 17:49 /mysql8/mysql_data/school/test2.ibd -rw-r-----. 1 mysql mysql 112K May 3 11:49 /mysql8/mysql_data/school/test.ibd [root@localhost ~]#
ibtmp1:12M:autoextend ibtmp1表示表空间文件名,autoextend表示是自增长
mysql> show variables like \'%innodb_temp%\'; +-----------------------------+-----------------------+ | Variable_name | Value | +-----------------------------+-----------------------+ | innodb_temp_data_file_path | ibtmp1:12M:autoextend | | innodb_temp_tablespaces_dir | ./#innodb_temp/ | +-----------------------------+-----------------------+ 2 rows in set (0.01 sec) mysql>
sysconfdir
[root@localhost ~]# cat /etc/my.cnf |grep port port=3306 [root@localhost ~]# netstat -ntlp |grep mysql tcp6 0 0 :::3306 :::* LISTEN 9577/mysqld tcp6 0 0 :::33060 :::* LISTEN 9577/mysqld [root@localhost ~]#
在/etc/mysql/my.cnf 里指定端口,优先级高于/etc/my.cnf
[root@localhost ~]# cat /etc/mysql/my.cnf [mysqld] port=3307 [root@localhost ~]# /etc/init.d/mysql restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS! [root@localhost ~]# netstat -ntlp |grep mysql tcp6 0 0 :::3307 :::* LISTEN 20340/mysqld tcp6 0 0 :::33060 :::* LISTEN 20340/mysqld [root@localhost ~]#
在多个配置文件有相同得参数时,优先级选最高的。
[root@localhost ~]# cat ~/.my.cnf [mysqld] port=3308 [root@localhost ~]# cat /etc/my.cnf |grep port port=3306 [root@localhost ~]# cat /etc/mysql/my.cnf |grep port port=3307 [root@localhost ~]# /etc/init.d/mysql restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS! [root@localhost ~]# netstat -ntlp |grep mysql tcp6 0 0 :::3308 :::* LISTEN 20733/mysqld tcp6 0 0 :::33060 :::* LISTEN 20733/mysqld [root@localhost ~]#
mysql> show variables like \'%innodb_data%\'; +-----------------------+------------------------+ | Variable_name | Value | +-----------------------+------------------------+ | innodb_data_file_path | ibdata1:12M:autoextend | | innodb_data_home_dir | | +-----------------------+------------------------+ 2 rows in set (0.12 sec)
[root@localhost ~]# vim /etc/my.cnf [root@localhost ~]# cat /etc/my.cnf [mysqld] basedir=/mysql8/mysql datadir=/mysql8/mysql_data log_error=/mysql8/mysql_data/LogError.log character-set-server=utf8 collation-server=utf8_unicode_ci port=3306 secure_file_priv=/tmp/ sql_mode=\'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\' innodb_data_file_path=ibdata1:12M:autoextend;ibdata2:30M#先把ibdata1的配置复制上去,然后新建个ibdata2初始化大小为30M [root@localhost ~]# [root@localhost ~]# /etc/init.d/mysql restart ERROR! MySQL server PID file could not be found! Starting MySQL. ERROR! The server quit without updating PID file (/mysql8/mysql_data/localhost.localdomain.pid). [root@localhost ~]#
结果启动报错了,查看日志,看意思是只有最新的表空间才能配置autoexitend,所以ibdata1的自增长要去掉。
2021-05-19T23:28:07.937740Z 0 [ERROR] [MY-012259] [InnoDB] Invalid File Path Specification: \'ibdata1:12M:autoextend;ibdata2:30M\'. Only the last file defined can be \'auto extend\'. 2021-05-19T23:28:07.937844Z 0 [ERROR] [MY-012370] [InnoDB] Unable to parse innodb_data_file_path=ibdata1:12M:autoextend;ibdata2:30M 2021-05-19T23:28:07.937882Z 0 [ERROR] [MY-010202] [Server] Plugin \'InnoDB\' init function returned error. 2021-05-19T23:28:07.937899Z 0 [ERROR] [MY-010734] [Server] Plugin \'InnoDB\' registration as a STORAGE ENGINE failed. 2021-05-19T23:28:07.937913Z 0 [ERROR] [MY-010168] [Server] Failed to initialize builtin plugins. 2021-05-19T23:28:07.938082Z 0 [ERROR] [MY-010119] [Server] Aborting
[root@localhost ~]# vim /etc/my.cnf [mysqld] basedir=/mysql8/mysql datadir=/mysql8/mysql_data log_error=/mysql8/mysql_data/LogError.log character-set-server=utf8 collation-server=utf8_unicode_ci port=3306 secure_file_priv=/tmp/ sql_mode=\'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\' innodb_data_file_path=ibdata1:12M;ibdata2:30M
修改后,启动成功,且多了个ibdatat2表空间文件
[root@localhost ~]# /etc/init.d/mysql restart ERROR! MySQL server PID file could not be found! Starting MySQL... SUCCESS!
[root@localhost ~]# ll -h /mysql8/mysql_data/ibd*
-rw-r-----. 1 mysql mysql 12M May 20 07:38 /mysql8/mysql_data/ibdata1
-rw-r----- 1 mysql mysql 30M May 20 07:38 /mysql8/mysql_data/ibdata2
[root@localhost ~]#
[root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 8 Server version: 8.0.13 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement. mysql> show variables like \'%innodb_data%\'; +-----------------------+-------------------------+ | Variable_name | Value | +-----------------------+-------------------------+ | innodb_data_file_path | ibdata1:12M;ibdata2:30M | | innodb_data_home_dir | | +-----------------------+-------------------------+ 2 rows in set (0.01 sec) mysql>
已经创建好的表空间文件是不能随意修改的,比如:
innodb_data_file_path=ibdata1:12M;ibdata2:60M#ibdata2从30M改成60M [root@localhost ~]# /etc/init.d/mysql restart Shutting down MySQL..... SUCCESS! Starting MySQL.. ERROR! The server quit without updating PID file (/mysql8/mysql_data/localhost.localdomain.pid).#启动失败 [root@localhost ~]# 2021-05-19T23:42:31.538771Z 1 [ERROR] [MY-012264] [InnoDB] The innodb_system data file \'./ibdata2\' is of a different size 1920 pages (rounded down to MB) than the 3840 p ages specified in the .cnf file! 2021-05-19T23:42:31.538834Z 1 [ERROR] [MY-012930] [InnoDB] Plugin initialization aborted with error Generic error. 2021-05-19T23:42:32.143128Z 1 [ERROR] [MY-010334] [Server] Failed to initialize DD Storage Engine 2021-05-19T23:42:32.143388Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed. 2021-05-19T23:42:32.143406Z 0 [ERROR] [MY-010119] [Server] Aborting
这个错误日志大概意思是说ibdata2当前有1920个数据页大小,比.cnf文件里少了3840页。
想要增加表空间大小,有两个方法,一是新增表空间文件,二十设置自增长。
innodb_data_file_path=ibdata1:12M;ibdata2:30M:autoextend [root@localhost ~]# /etc/init.d/mysql restart ERROR! MySQL server PID file could not be found! Starting MySQL.. SUCC
[root@localhost ~]# cat /etc/my.cnf |grep innodb_data_file innodb_data_file_path=ibdata1:12M;ibdata2:30M:autoextend:max:20G [root@localhost ~]# /etc/init.d/mysql restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 8 Server version: 8.0.13 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement. mysql> show variables like \'%innodb_data%\'; +-----------------------+--------------------------------------------+ | Variable_name | Value | +-----------------------+--------------------------------------------+ | innodb_data_file_path | ibdata1:12M;ibdata2:30M:autoextend:max:20G | | innodb_data_home_dir | | +-----------------------+--------------------------------------------+ 2 rows in set (0.17 sec) mysql>
[root@localhost ~]# ll -h /mysql8/mysql_data/ib_log* -rw-r-----. 1 mysql mysql 48M May 20 12:18 /mysql8/mysql_data/ib_logfile0 -rw-r-----. 1 mysql mysql 48M Jun 27 2020 /mysql8/mysql_data/ib_logfile1 [root@localhost ~]# mysql> show variables like \'%innodb_log%\'; +------------------------------------+----------+ | Variable_name | Value | +------------------------------------+----------+ | innodb_log_buffer_size | 16777216 | | innodb_log_checksums | ON | | innodb_log_compressed_pages | ON | | innodb_log_file_size | 50331648 | | innodb_log_files_in_group | 2 | | innodb_log_group_home_dir | ./ | | innodb_log_spin_cpu_abs_lwm | 80 | | innodb_log_spin_cpu_pct_hwm | 50 | | innodb_log_wait_for_flush_spin_hwm | 400 | | innodb_log_write_ahead_size | 8192 | +------------------------------------+----------+ 10 rows in set (0.00 sec) mysql>
mysql> select 50331648/1024/1024;
+--------------------+
| 50331648/1024/1024 |
+--------------------+
| 48.00000000 |
+--------------------+
1 row in set (0.00 sec)
mysql>
[root@localhost ~]# ll -h /mysql8/mysql_data/undo* -rw-r-----. 1 mysql mysql 11M May 20 12:18 /mysql8/mysql_data/undo_001 -rw-r-----. 1 mysql mysql 11M May 20 12:18 /mysql8/mysql_data/undo_002 [root@localhost ~]#
mysql> show variables like \'%innodb_undo%\'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_undo_directory | ./ | | innodb_undo_log_encrypt | OFF | | innodb_undo_log_truncate | ON | | innodb_undo_tablespaces | 2 | +--------------------------+-------+ 4 rows in set (0.01 sec) mysql>
mysql> show variables like \'%innodb_temp%\'; +-----------------------------+-----------------------+ | Variable_name | Value | +-----------------------------+-----------------------+ | innodb_temp_data_file_path | ibtmp1:12M:autoextend | | innodb_temp_tablespaces_dir | ./#innodb_temp/ | +-----------------------------+-----------------------+ 2 rows in set (0.00 sec)
mysql> SHOW STATUS LIKE \'Innodb_buffer_pool_dump_status\'; +--------------------------------+------------------------------------+ | Variable_name | Value | +--------------------------------+------------------------------------+ | Innodb_buffer_pool_dump_status | Dumping of buffer pool not started | +--------------------------------+------------------------------------+ 1 row in set (0.00 sec) mysql> SHOW STATUS LIKE \'Innodb_buffer_pool_load_status\'; +--------------------------------+--------------------------------------------------+ | Variable_name | Value | +--------------------------------+--------------------------------------------------+ | Innodb_buffer_pool_load_status | Buffer pool(s) load completed at 210521 9:27:15 | +--------------------------------+--------------------------------------------------+ 1 row in set (0.00 sec) mysql>
以上是关于mysql中innodb数据库事务修改多记录的主要内容,如果未能解决你的问题,请参考以下文章