Mysql functions, transactions, views, logs, master-slave replication

Mysql function

Character function

# 获取字节数
select length('join') # 为4
select length('张三丰') # 9
show variables like '%char%' 
# 可查询到我们使用的是utf8字符集 
# utf8一个汉字三个字节
# gbk一个汉字两个字节

# 拼接字符串
select concat(last_name,_,first_name) from employees

#大小写
select upper('john');
select lower('JHON');

#将姓变大写 名变小写 然后拼接
select concat(last_name,'_',first_name) 姓名 from employee;

# 截取字符 注意索引从1开始 
# 这个数字是指字符长度而不是字节长度
select substr('rush',2); # ush
select substr('rush',2,3); # us

# 查找位置
select instr('rush','sh'); # 3 如果找不到返回0

# 去掉某字符
select trim('   rush    ');
# 去空格 rush
select trim('r' from 'rush');
# ush
            
# 插入字符 这是向左填充 字符是指插入后总长度 相似的是rpad 数字太小了会截断

select lpad('rush','6','!');
# !!rush
         
#replace 替换
select replace('rush','sh','a')
# rua

Mathematical function

# round 四舍五入
select round(1.45);  #1
select round(1.6);  #2
select round(-1.6);  #-2
select round(1.567,2) #保留两位

#ceil 向上取整
select ceil(1.002); #2
select ceil(1.00); #1

#floor 向下取整
select ceil(1.002); #1
select ceil(1.00); #1
select ceil(-9.99) #-10

#truncate 截断
select truncate(1.6999,1); #1.6

#mod取余
select mod(10,3); #1
select mod(-10,-3); #-1
select mod(10,-3); #1

Date function

#now 返回当前系统日期+时间
select now();

#curdate 返回当前系统日期,不包含时间
select curdate();

#curtime 返回当前时间 不包含日期
select curtime();

#可以获取指定的部分 年、月、日、小时、分钟、秒
select year(now()) 年;
select year('1998-1-1') 年;

#转换日期函数
select str_to_date('1998-3-2','%Y-%c-%d');

#根据日期查询
select * from employees where hiredate = str_to_date('4-3 1992','%c-%d %Y');

#将日期转换成字符
select date_format(now(),'%y年%m月%d日') 

image-20200725132046712

Flow control function

#起到if-else的效果
select if(10<5,'大','小');

#switch-case
select salary,department_id,
case department_id
when 30 then salary*1.1
when 40 then salary*1.2
else salary 
end 
from employees;

select salary 
case 
when salary>20000 then 'A'
when salary>15000 then 'B'
when salary>10000 then 'C'
else 'D'
END
from employees

Affairs

​ If a business operation that contains multiple steps is managed by the transaction, it will either succeed or fail at the same time. (Bank transfer)
operation:

开启事务:start transaction; 
回滚:roll back;
提交:commit;

start transaction;
update account set balance = 1000;
update account set balance =banlance - 500 where name = 'zhangsan';
update account set balance =banlance + 500 where name = 'lisi';
commit;
roll back;

Automatic submission

​ mysql automatically commits by default. If a start transaction is run in the statement, it must be committed, otherwise, it is a temporary state, and the data will be rolled back after it is closed.
If the transaction has been opened, an addition, deletion, and modification will be automatically committed once.

Modify the default submission method

查看 select @@autocommit; --1自动 0手动
修改: set  @@autocommit = 0;

Four characteristics of business

  • Atomicity: It is the smallest unit of indivisible operation that either succeeds or fails at the same time.
  • Persistence: The data will always stay after rollback or submission.
  • Isolation: Multiple transactions are independent of each other.
  • Consistency: The total amount of data remains unchanged before and after the transaction operation.

Transaction isolation level

​ Multiple transactions are isolated and independent of each other, but if multiple transactions operate on the same batch of data, it will cause some problems, and setting different isolation levels can solve these problems.

  • 1. Dirty read: One transaction reads data that is not committed by another transaction. (two windows)
  • 2. Non-repeatable read: In a transaction, the data read twice is different.
  • 3. Phantom reading: refers to the fact that when a transaction queries the same range twice before and after, the latter query sees rows that the previous query did not see.

Isolation level

read uncommitted: read uncommitted
problems: dirty read, non-repeatable read, phantom read
read committed: read committed (oracle default)
will generate problems: non-repeatable read, phantom read
repeatable read: repeatable read (mysql default )
Problems that will arise: phantom reading
serializable: serialization
can solve all problems.Note
: the isolation level is getting higher and higher, and the efficiency is getting lower and lower.

Query isolation: select @@tx_isolation;
setting: set global transaction isolation level level string; (such as read uncommitted)
and, after setting, reopen sqlyog!!!

Mysql view

Views are virtual tables, temporary tables

The data dynamically generated by the table only saves the sql logic, not the query results.

create

create view v1
as 
select stuname,majorname
from stuinfo s
inner join major m on s.'majorid'=m.'id';

select * from v1 where stuname like '张%'

modify

create or replace view 视图名
as 。。。。

alter view 视图名
as 。。。。

Delete (requires permission)

drop view 视图名

View view

desc 视图名
show create view 视图名

Mysql log

The mysql log is mainly divided into

  • Error log
  • Query log
  • Slow query log
  • Transaction log
  • Binary log

Error log

​ In Mysql database, the error log function is turned on by default and cannot be turned off. The error log is stored in the data file of the mysql database by default. The error log file name is usually hostname.err.

​ Error log records: server startup and shutdown information, operating error information, information generated when the time scheduler runs an event, and information generated by a process started on the server.

Query log

​ By default, the query log is turned off. If it is turned on under high concurrency, a large amount of unnecessary data will be generated, which will affect the performance of mysql.

Slow log

​ Used to record query statements whose execution time exceeds the specified time. The impact on server performance is small. It also records the obstruction caused by other resources being requisitioned.

Transaction log

​ Unique to InnoDB, it can help improve transaction efficiency. Using the transaction log, the storage engine only needs to modify the log copy when modifying the data of the table, and then record the modification behavior in the transaction log of the persistent hard disk, instead of persisting the modified data itself to the disk every time. If the data modification has been recorded in the transaction log and persisted, but the data itself has not been written back to the disk, and the system crashes at this time, the storage engine can automatically reply to this part of the modified data when restarting.

Binary log

​ Also called change log, it is mainly used to record mysql statements that modify data or may cause data changes, and record the time of statement occurrence, execution time, operation data, and so on.

View log

mysql> show global variables like '%log%';
+-----------------------------------------+--------------------------------+
| Variable_name                           | Value                          |
+-----------------------------------------+--------------------------------+
| back_log                                | 250                            |
| binlog_cache_size                       | 32768                          |
| binlog_checksum                         | CRC32                          |
| binlog_direct_non_transactional_updates | OFF                            |
| binlog_error_action                     | IGNORE_ERROR                   |
| binlog_format                           | STATEMENT                      |
| binlog_gtid_simple_recovery             | OFF                            |
| binlog_max_flush_queue_time             | 0                              |
| binlog_order_commits                    | ON                             |
| binlog_row_image                        | FULL                           |
| binlog_rows_query_log_events            | OFF                            |
| binlog_stmt_cache_size                  | 32768                          |
| binlogging_impossible_mode              | IGNORE_ERROR                   |
| expire_logs_days                        | 0                              |
| general_log                             | OFF                            |
| general_log_file                        | /var/lib/mysql/kafka2.log      |
| innodb_api_enable_binlog                | OFF                            |
| innodb_flush_log_at_timeout             | 1                              |
| innodb_flush_log_at_trx_commit          | 2                              | ===>【事务日志】详解[1]
| innodb_locks_unsafe_for_binlog          | OFF                            |
| innodb_log_buffer_size                  | 33554432                       |
| innodb_log_compressed_pages             | ON                             |
| innodb_log_file_size                    | 536870912                      |
| innodb_log_files_in_group               | 2                              | ===>【事务日志】至少2个
| innodb_log_group_home_dir               | ./                             | ===>【事务日志】定义innodb事务日志组的文件目录
| innodb_mirrored_log_groups              | 1                              | ===>【事务日志】表示对日志组做镜像
| innodb_online_alter_log_max_size        | 134217728                      |
| innodb_undo_logs                        | 128                            |
| log_bin                                 | OFF                            |
| log_bin_basename                        |                                |
| log_bin_index                           |                                |
| log_bin_trust_function_creators         | OFF                            |
| log_bin_use_v1_row_events               | OFF                            |
| log_error                               | ./kafka2.err                   | ===>【错误日志】错误日志输出目录以及错误日志文件名
| log_output                              | FILE                           |
| log_queries_not_using_indexes           | OFF                            |
| log_slave_updates                       | OFF                            |
| log_slow_admin_statements               | OFF                            |
| log_slow_slave_statements               | OFF                            |
| log_throttle_queries_not_using_indexes  | 0                              |
| log_warnings                            | 1                              | ===>【错误日志】是否把警告信息添加进错误日志中 
| max_binlog_cache_size                   | 18446744073709547520           |
| max_binlog_size                         | 1073741824                     |
| max_binlog_stmt_cache_size              | 18446744073709547520           |
| max_relay_log_size                      | 0                              |
| relay_log                               |                                |
| relay_log_basename                      |                                |
| relay_log_index                         |                                |
| relay_log_info_file                     | relay-log.info                 |
| relay_log_info_repository               | FILE                           |
| relay_log_purge                         | ON                             |
| relay_log_recovery                      | OFF                            |
| relay_log_space_limit                   | 0                              |
| simplified_binlog_gtid_recovery         | OFF                            |
| slow_query_log                          | OFF                            | ===>【慢日志】查看慢日志是否开启
| slow_query_log_file                     | /var/lib/mysql/kafka2-slow.log | ===>【慢日志】查看慢日志的文件目录以及文件名
| sql_log_bin                             | ON                             |
| sql_log_off                             | OFF                            |
| sync_binlog                             | 0                              |
| sync_relay_log                          | 10000                          |
| sync_relay_log_info                     | 10000                          |
+-----------------------------------------+--------------------------------+
61 rows in set (0.00 sec)

View slow log

show global variables like 'long%';

View binary log

show global variables like "%log_bin%";

Mysql master-slave replication

​ With the increase in system application access, the pressure of database read and write access increases. When a certain bottleneck is reached, the database read and write efficiency becomes very low. We usually use Mysql cluster to solve such problems.

What is master-slave replication

​ In order to reduce the pressure on the main library, read and write separation should be done at the system application level. The main library is used for write operations and the sub-library is used for read operations.

Separate read and write and master-slave synchronization, a dispersion pressure database access, to enhance the performance and availability of the entire system, reducing the failure rate of a large amount of the initiator to access the database downtime.

​ The result of the replication is that the data of all database servers in the cluster is theoretically the same, and they are all the same data. The default built-in replication strategy of Mysql is asynchronous, based on different configurations, Slave (sub-database) It is not necessary to keep a connection with the Master (main database) for continuous replication or waiting for replication. We can specify to replicate all databases, some databases, or even some tables of a certain database.

Copy strategy

Mysql replication supports many different strategies.

  • Synchronization strategy

Master must wait for all slaves to respond before submitting

The submission of Mysql to DB operation is usually to write the binary log file of the operation event first and then submit

  • Semi-synchronous strategy

Master can submit as long as it waits for a Slave

  • Asynchronous strategy

Master does not need to wait for Slave

  • Delay strategy

Set the time that Slave is behind Master

Copy mode

According to the different binlog log format, Mysql replication supports multiple different replication modes at the same time

  • Statement-based replication, namely Statement Based Replication (SBR), records each sql that changes data
    • Advantages: binlog file is smaller, saving I/O, and higher performance.
    • Disadvantages: Not all data changes are written to binlog. Some special functions and uncertain statement operations will cause the problem that master-slave data cannot be replicated.
  • Row-based replication, namely Row Based Replication (RBR): Does not record sql, only records the details of each row of data changes.
    • Advantages: comprehensive and detailed, recording the details of each line
    • The large amount of binlog log data and poor performance increase the probability of master-slave synchronization delay.
  • Hybrid replication, that is, the general statement modification uses the statement format to save the binlog, but for some functions, the statement cannot complete the master-slave replication operation, and the row format is used to save the binlog. Simply put, Mysql will choose one of Statement and row to save.

Advantages of master-slave replication

performance

​ Mysql replication is a scale-out solution (horizontal expansion) that spreads the original single point load to multiple slave machines, thereby improving the overall service performance. The separation of reading and writing makes the functions more specific and improves the overall service performance to a certain extent.

Recovery

​ One Slave can be read from other Slaves when it is down (the data is the same in theory). If master-slave switching is configured, the Master can also use the Slave to write when it is down, so that operation and maintenance personnel will not despair.

data analysis

​ Using Slave data analysis, Master continues to work, so that it will not affect Master performance.

Guess you like

Origin blog.csdn.net/Rush6666/article/details/107589624