mysql database lock mechanism and master-slave replication

The classification of mysql locks:
for data operation types, it is divided into read locks and write locks;
for the granularity of data operations, it is divided into table locks and row locks; for
table locks: it is biased towards the MyISAM storage engine, with low overhead, plus Fast locking; no deadlock, large lock granularity, the highest probability of lock conflicts, and the lowest concurrency;

show open tables; view table information

After locking and reading lock, you can only read the table, not write to the table;

Table lock

Locking command:
add read lock: lock table mylock read;
note:
when multiple windows are operating on the same table; blocking will occur until the lock is obtained for operation,
if the same window is on a table Lock, then the window can not operate other tables;
add write lock:
lock table mylock write;
Note: the
same window, you can query and write
to the table ; different windows are reading and writing to the table Will cause blocking until the lock is released;

Unlock
command to unlock: unlock table mylock;

Row lock

Prefers to the innodb storage engine, high overhead, slow locks, deadlocks, the smallest lock granularity, the lowest probability of lock conflicts, and the highest concurrency.
The biggest difference between innodb and myisam is 1. Support transactions 2. Support row-level locks
The acid characteristic of the transaction,
atomicity: A transaction is an operating unit, and its data modification is either all executed or not executed.
Persistence: After the transaction is completed, its operation on the data is permanent
isolation: the database provides a certain The isolation mechanism ensures that transactions are executed in an "independent" environment that is not affected by external concurrent operations. The intermediate state in the transaction processing process is invisible to the outside. The
consistency: at the beginning and completion of the transaction, the data must be Maintain a consistent state, which means that all related operations must be applied to transaction modifications to maintain data integrity.

Data conflict caused by concurrency

First introduce a few concepts:
transaction isolation level and security level:
读未提交 < 读已提交 < 可重复读 < 序列化读

Dirty read: Modified but not yet committed, transaction a reads the data that has been modified but not committed by transaction b.
Non-repeatable read: when the data that has been read is read again, the data has changed;
phantom read: transaction a The newly added data of transaction b is read, provided that the two transactions occur simultaneously. Does not meet the isolation of the transaction;

View the transaction isolation level of the current database:
show variables like “tx_isolation”;

Regarding how mysql implements row-level locks,
mysql implements row-level locks through the mvcc multi-version control protocol. In-
depth understanding of the four isolation levels and underlying implementation principles of mysql (MVCC and locks)

After the index fails, the row lock changes to the table lock; mysql defaults to lock based on the index. Row locks are locks established on the index, which will become table locks after the index fails

What is a gap lock?

When we retrieve data using range conditions instead of equal conditions and request shared or exclusive locks, InnoDB will lock the index entries of existing data records that meet the conditions; for records whose key values ​​are within the condition range but do not exist, Called gap

Innodb will also lock this gap, this locking mechanism is the so-called gap lock

Harm: Because the query searches through the range during execution, it will lock all index key values ​​in the entire range, even if the key value does not exist.
Gap lock has a fatal weakness, that is, after locking a range of key values, even some non-existent key values ​​will be innocently locked, causing no data in the locked range to be inserted when locked.
For example, our database has data in the id range 1-6 but no id=5 value. When we add id=5 value in another window, blocking will occur;
Insert picture description here

How to lock a row

After the statement, add a query for a row and add for update at the back; this will add a lock to this row;
in the subsequent operations, only we can manipulate the data of this row by ourselves, and the others need to be blocked wait;

Master-slave replication

The basic principle of replication:

  1. The slave will read the binlog from the master to synchronize data. These recording processes are called binary log events;
  2. The slave copies the master's binary log events to its relay log;
  3. slave relay redo logs event that will change the application to its own database, mysql replication is asynchronous and serialized Insert picture description here
    basic rule of copy
  4. Each slave has only one master
  5. Each master can only have one unique server ID
  6. Each master can have multiple slaves

Common configuration of master-slave configuration

  • The mysql version is consistent and the background runs as a service
  • The master and slave are configured under the [mysqld] node, all in lowercase
  • Configure a unique id server-id=1 on the main mysql server
  • Configure the location of logbin
  • log-bin=D:/mysql-8/data/mysqlbin
  • log-err=D:/mysql-8/data/mysqlerr
  • basedir=D:/mysql-8/
  • tmpdir=D:/mysql-8/
    configuration of slave database:
    turn off server-id=1 and turn on server-id=2

Authorize the slave to log in to the host;
grant replication slave on . To "zhangsan"@"from the machine database ip" identifed by "password"';
refresh permissions: flush preivileges;
Insert picture description here
configure the slave:
tell the slave's database from the host Where to start the backup
command

change master to master_host = "192.168.14.165",
master_user="zhangsan",
master_password ="123456",
master_log_file="mysqlbin.000035",master_log_pos=341;

Then start the slave

start slave\G;

Insert picture description here
In this way, every time the master inserts, updates, and deletes the database, it will be synchronized to the slave.

Guess you like

Origin blog.csdn.net/qq_43079376/article/details/106503309