死磕mysql(4)

想把论坛和博客上所有关于mysql的都看一遍,死磕到底

看到关于数据库快照的东西。。。。。。。不懂,百度。。。。。。然后就跑题了,看到了表锁这种东西unlock tables;

用来锁定表。。。。。

mysql> insert into new values('haha'); ERROR 1136 (21S01): Column count doesn't match value count at row 1 mysql> desc new; +-------+-------------+------+-----+---------+-------+ | Field | Type        | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id    | int(11)     | NO   | PRI | NULL    |       | | name  | varchar(20) | YES  | MUL |         |       | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.36 sec)

mysql> insert into new values(1234,'haha'); ERROR 1099 (HY000): Table 'new' was locked with a READ lock and can't be updated

mysql> unlock tables new ; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'new' at line 1 mysql> unlock tables ; Query OK, 0 rows affected (0.00 sec)

mysql> insert into new values(1235,'haha'); Query OK, 1 row affected (0.94 sec)

--lock tables new read;读锁,一旦锁上就不应许其他线程的写操作了。。。

--lock tables new write;xie锁,一旦锁上就不应许其他线程的读操作了。。。

网上还看到这么一句话,可是和我的测试相违背

//注意:user表必须为Myisam表,以上测试才能全部OK,如果user表为innodb表,则lock tables user read local命令可能没有效果,也就是说,如果user表为innodb表,第6时刻将不会被阻塞,这是因为INNODB表是事务型的,对于事务

表,例如InnoDB和BDB,--single-transaction是一个更好的选项,因为它不根本需要锁定表//

违背

看一下我的数据库引擎

mysql> show create table newG;
*************************** 1. row ***************************
       Table: new
Create Table: CREATE TABLE `new` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

并没有出现异常,innodb数据库引擎也可以使用表锁

推测是数据库版本所产生的差异

mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.15-log |
+------------+
1 row in set (0.00 sec)

再次测试使用myisam

忘记关掉表锁了,还发现了一个问题,不光是表的问题了,而且连数据库表的创建都不被锁定了,超出我的意料。

mysql> create table new3(id int primary key , name varchar(20) default '')engine
=myisam,charset=utf8;
ERROR 1100 (HY000): Table 'new3' was not locked with LOCK TABLES

再次做出推测,不光是数据库的表的整个操作都被干扰 了,只允许read,

测试表的修改是否还可以。

mysql> alter table new1 add haha int;
ERROR 1100 (HY000): Table 'new1' was not locked with LOCK TABLES

果然如此啊,只允许read了,修改,添加,表的结构修改也被锁定了

回来测试myisam

mysql> create table new3(id int primary key , name varchar(20) default '')engine
=myisam,charset=utf8;
Query OK, 0 rows affected (0.06 sec)

添加成功,已经关闭锁了

mysql> show create table new3G;
*************************** 1. row ***************************
       Table: new3
Create Table: CREATE TABLE `new3` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> insert into new3 values(12346,'asdf');
ERROR 1099 (HY000): Table 'new3' was locked with a READ lock and can't be update
d

无法插入

mysql> update new3 set name = '123dsfv' where id=1;
ERROR 1099 (HY000): Table 'new3' was locked with a READ lock and can't be update
d

无法更新

mysql> alter table new3 add haha int;
ERROR 1099 (HY000): Table 'new3' was locked with a READ lock and can't be update
d

无法修改表

| 1233 | name |
| 1234 | name |
+------+------+
1235 rows in set (0.00 sec)

可以查找

对照博客http://blog.chinaunix.net/uid-21505614-id-289450.html

原文地址:https://www.cnblogs.com/ututuut/p/4335820.html