Learn Mysql (c)

MySQL lock

What is Mysql lock?

Computer lock mechanism to coordinate multiple processes or threads concurrent access to a resource. In the database, in addition to traditional computing resources (such as CPU, memory, I / O, etc.) than contention, the data is also a shared resource for many users. How to ensure the consistency of concurrent access to data, the validity of all databases an important factor in a problem that must be addressed, lock conflicts also affect the performance of concurrent access to the database. Mysql to use a lot of this locking mechanism, such as row locks, table locks, read locks, write locks, are locked in before the first operation these locks are collectively referred to as pessimistic locking (pessimistic lock)

 

The basic description of lock

 

MySQL lock these three characteristics may be roughly summarized as follows:

 

Table-level lock: small overhead, lock fast; not deadlock; lock large size, the probability of lock conflicts of the highest and lowest degree of concurrency

Row-level locking: spending big, locked slow; there will be a deadlock; locking granularity smallest and lowest probability of lock conflicts, but also the highest degree of concurrency

Page locks: the cost and the locking time is between tables and row locks; deadlock occurs; particle size between locking and lock table row lock, in general concurrency

 

Table-level locking

MySQL table-level locking has two modes: the table shared read lock ( the Table the Read Lock ) and table exclusive write locks ( the Table the Write Lock ).

 

MySQL table level lock contention state variables implemented: Show Status like '% Table';

table_locks_immediate: frequency generating table-level locking;

table_locks_waited: table-level locking appears waiting times occur contention;

 

 

Table lock demo

 

Read lock demo

 

 

 

 

 

 

 

 

 

 Write Lock demo

 

 

Row lock

Read lock: read lock allows other threads, but does not allow write lock on.

 

 

 写锁:不允许其他线程上任何锁。

 

 

 

 行锁必须要索引才能实现,否则会自动锁全表,两个事务可以用同一个索引,下面给出例子:
读锁由于不排除其他线程再加读锁比较难测试,所以下面用写锁测试,先测试没加所以字段进行加锁,对不同记录进行加锁,如果都加锁成功说明是加了行锁,反之则是默认锁全表。
下面的consumer_chain_order_number是不会重复的,但没有索引

 

-- 马上显示查询结果
BEGIN;
SELECT * from tyg_consumer_chain_sell_order o where o.consumer_chain_order_number=26911523448454 for update

--一直没有结束知道等待超时
BEGIN;
SELECT * from tyg_consumer_chain_sell_order o where o.consumer_chain_order_number=55181523448554 for update

 

说明了默认锁全表,接下来试下有索引的字段,这个表的主键

 

 

 

 

 

集群搭建之主从复制

Mysql主从复制过程的图形表示

 

 

 主服务器配置

 修改my.conf文件

 

 

我起的名叫mysql-bin,到时会生成mysql-bin.000001这样的文件

server-id一般和机器ip尾数一样就行,我的是3

重启mysql服务

service mysqld restart

建立帐户并授权slave

 

 

 

 

 

 

 

一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.145.226,加强安全。

我这边为了演示,用了root账号。让从机通过主机的用户名/密码 root/root登录进入

然后刷新权限

  查询master的状态

 

 

position=602这个偏移量记住,后面会用到

之前的设置中,发现在/var/lib/mysql文件夹下,已经多出了mysql-bin开头的bin log日志文件

 

 

 

从服务器配置

修改my.conf文件

 

 

 删除UUID文件

 

重启并登录到MySQL进行配置从服务器

mysql>change master to master_host='192.168.10.133',master_port=3306,master_user='root',master_password='root',master_log_file='mysql-bin.000001',master_log_pos=569 

 

注意语句中间不要断开,master_port为mysql服务器端口号(无引号),master_user为执行同步操作的数据库账户,“120”无单引号(此处的120就是show master status 中看到的position的值,这里的mysql-bin.000001就是file对应的值)。

 

   启动从服务器复制功能

 

  

 

  

   检查从服务器复制功能状态

 

   

Guess you like

Origin www.cnblogs.com/ptxxc/p/12118222.html