SQL Advanced (2) - MySQL locking problem and encoding related issues

1.mysql coding

1.1, mysql coding View

mysql> show variables like 'character%';
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| Variable_name | Value |
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+

1.2, provided the coding mysql

# vi /etc/my.cnf
如下(少补)[mysqld]
character‐set‐server=utf8
collation‐server=utf8_general_ci
sql_mode='NO_ENGINE_SUBSTITUTION'
[mysql]
default‐character‐set = utf8
[mysql.server]
default‐character‐set = utf8
[mysqld_safe]
default‐character‐set = utf8
[client]
default‐character‐set = utf8
重启mysql
# service mysqld restart
# 或者使用 service mysql restart
再次查看编码:
# mysql ‐uroot ‐p
mysql> show variables like 'character%';
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| Variable_name | Value |
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
8 rows in set (0.00 sec)

1.3 mysql directory and configuration files Description

A, /etc/my.cnf which is mysql main configuration file
B, / var / lib / mysql mysql database file database storage position

[root@mysql etc]# cd /var/lib/mysql/
[root@mysql mysql]# ll
total 176172
‐rw‐rw‐‐‐‐. 1 mysql mysql 56 Oct 20 00:10 auto.cnf
‐rw‐rw‐‐‐‐. 1 mysql mysql 79691776 Oct 21 04:46 ibdata1
‐rw‐rw‐‐‐‐. 1 mysql mysql 50331648 Oct 21 04:46 ib_logfile0
‐rw‐rw‐‐‐‐. 1 mysql mysql 50331648 Oct 20 00:09 ib_logfile1
drwx‐‐‐‐‐‐. 2 mysql mysql 4096 Oct 21 01:38 itcast
drwx‐‐‐‐‐‐. 2 mysql mysql 4096 Oct 21 04:45 menagerie
drwx‐‐x‐‐x. 2 mysql mysql 4096 Oct 20 00:09 mysql
‐rw‐r‐‐‐‐‐. 1 mysql root 2219 Oct 20 00:14 mysql.err
‐rw‐rw‐‐‐‐. 1 mysql mysql 5 Oct 20 00:10 mysql.pid
srwxrwxrwx. 1 mysql mysql 0 Oct 20 00:10 mysql.sock
drwx‐‐‐‐‐‐. 2 mysql mysql 4096 Oct 20 00:09 performance_schema
‐rw‐r‐‐r‐‐. 1 root root 111 Oct 20 00:09 RPM_UPGRADE_HISTORY
‐rw‐r‐‐r‐‐. 1 mysql mysql 111 Oct 20 00:09 RPM_UPGRADE_MARKER‐LAST
drwxr‐xr‐x. 2 mysql mysql 4096 Oct 20 00:09 test

Our database mysql database file is usually stored in a ** / ver / lib / mysql ** this directory.
Log output storage location C, / var / log / mysql database
D, view port. Netstat -nltp see if you can find port 3306

[root@mysql menagerie]# netstat ‐nltp
Active Internet connections (only servers)
Proto Recv‐Q Send‐Q Local Address Foreign Address
State PID/Program name
tcp 0 0 0.0.0.0:57958 0.0.0.0:*
LISTEN 1306/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0:*
LISTEN 1284/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:*
LISTEN 1573/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:*
LISTEN 1362/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:*
LISTEN 1654/master
tcp 0 0 :::3306 :::*
LISTEN 37860/mysqld
tcp 0 0 :::49647 :::*
LISTEN 1306/rpc.statd
tcp 0 0 :::111 :::*
LISTEN 1284/rpcbind
tcp 0 0 :::22 :::*
LISTEN 1573/sshd
tcp 0 0 ::1:631 :::*
LISTEN 1362/cupsd
tcp 0 0 ::1:25 :::*
LISTEN 1654/master
[root@mysql mysql]# cat mysql.pid
37860

2. Database Concurrency control (locking)

2.1 lock concept:

First, let's understand under what is a database lock,

The lock is a transaction before access to a database Xianxiang proposed system resources (e.g., tables and records) request, the resource block, the transaction lock is obtained, i.e. assume control of the data, in the transaction releases its lock other transactions can not update this data. After the withdrawal of the transaction, release the locked resources.

2.2 Why lock?

数据库是一个多用户使用的共享资源,比如一个用户表 t_user,两个浏览器前面的人登录了同个一个账号,把电话号码改了。当多个用户并发地存取数据时,在数据库中就会产生多个事务同时存取同一数据的情况。若对并发操作不加控制就可能会读取和存储不正确的数据,破坏数据库的一致性(脏读,不可重复读,幻读等),可能产生死锁。为了解决这个问题,加锁是一个非常重要的技术,对实现数据库并发控制是一个好的方案。简单说,当一个执行sql语句的事务想要操作表记录之前,先向数据库发出请求,对你访问的记录集加锁,在这个事务释放这个锁之前,其他事务不能对这些数据进行更新操作。

2.3 database locks Category:

1. Optimistic locking:

Optimistic locking is not built-in database, we need to realize themselves. Optimistic locking means that when the operation of the database (update), the idea is very optimistic that this operation will not lead to conflict, when working with data, not any other special treatment (that is, not locked), while making after the update, go to determine whether there is a conflict.
Typically this is implemented: when the data in the table to operate (update), the data table plus give a version (Version) field, every time the operation, the piece of recording the version number is incremented. That is to check out the piece of history, get a version field, if you want to operate (updated) for that record, the first version of the judgment value at the moment is equal to the value of the version of the time just check out, if they are equal, then the during this period, no other programs to manipulate it, you can perform the update, the version field's value plus 1; the value at the moment of discovery version version of the value is not equal to just get out if the update, then have this period there are other programs that manipulate, and update operation is not performed.
Example:
single operation comprises three steps:
1. queried product information

select (status,price,version) from t_goods where id=#{id}

2. The commodity information generating line
3. Product Review status 2

update t_goods

set status=2,version=version+1

where id=#{id} and version=#{version};

2. pessimistic lock:

And optimism is pessimistic lock corresponding lock. Pessimistic locking is when working with data that this data conflicts occur, it must operate on the same data by obtaining the lock can during each operation, this is very similar to java in synchronized, so pessimistic locks need spend more time. In addition to the corresponding lock optimism, pessimistic locking is achieved by the database itself, use the time, we directly call the related statements database on it.
Here, relates pessimistic locking to lock the other two concepts came out, they are shared locks and exclusive locks. Shared locks and exclusive locks are different implementations pessimistic locking , pessimistic locking Talia belong to the scope of.

Shared locks:

S known locks or read locks, plus a shared lock data objects can be read by other transactions, but can not modify , typically the data object is read is completed, the lock is released immediately

Exclusive lock:

Also known as X lock or read lock, when data object is combined with exclusive lock, a transaction must be key in order to access the data object, until the end of the lock transaction were released . In between other transaction can not read and modify it.

3. Line Lock:

Row lock, understand the literal meaning, it is to add a row lock, which is a record plus lock.
For example, before the presentation of sharing the lock statement
SELECT * from city where id = " 1" lock in share mode;
due to the city table, id field as the primary key, it is also equivalent to the index. When performing lock, this index will id record 1 plus the lock, the lock is a row lock.

MySQL locking mechanism summary

1.mysql storage engine

MYISAM (lock table), INNODB (row locks)

2.MySQL lock classification

Page locks

Table lock: Lock large particle size, poor support for concurrency, deadlock will not cause
a read lock: When making inquiries of the current transaction, other transactions will not query operations
write lock: Current affairs during data modification operations time, other transactions can not modify the data

Line Lock: Lock small size, better support for concurrent performance, easily lead to deadlock, easy to trigger full-text search (indexed)
Share lock: read lock is similar to
an exclusive lock: write lock similar
intent locks
intent shared lock
intent exclusive lock

How to implement a distributed lock

Optimistic locking (Line Lock)
is controlled based on the data version number

Pessimistic lock (lock table)
based on the realization of the table

3. For the mysql stuff or resume learning needs

sql optimization, performance optimization, index, (b + trees, red-black tree)

4. Other functions implemented method lock

Redis
SETNX ()
distributed lock frame: redssion

ZooKeeper (maximum) split brain, sheep
node znode

Published 122 original articles · won praise 1 · views 7328

Guess you like

Origin blog.csdn.net/qq_36079912/article/details/104524086