死磕mysql(4)

來源:互聯網
上載者:User

標籤:

想把論壇和部落格上所有關於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 new\G;
*************************** 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 new3\G;
*************************** 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

 

死磕mysql(4)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.