当前位置:网站首页>Mysql database
Mysql database
2022-07-03 06:10:00 【vincentwc】
Mysql 5.7
DML
replace
Before inserting a record , It is necessary to confirm whether the value of a field of this record in the table is repeated , If repeat , Need to update other columns , If there is no repetition , Just insert
replace into t(id,update_time) values(1,now());
Re execution replace into When ,mysql We will first look for the primary key or unique key to determine whether the records conflict , If conflict , Is executed delete+insert operation , If there is no conflict, execute insert operation
stay mysql In some versions of , When there is conflict , The main library will execute delete+insert operation , and binlog It's recorded in update operation , This leads to the self increment of the slave Library id Not in line with reality . Manual adjustment is required during master-slave switching
DDL
- The design principle of the table
- See the name and know the meaning
- Satisfaction paradigm
- Reasonable model selection
- Character set selection utf8,utf8mb64
- Engine selection innodb
- Row lock
- High performance
- Support transactions
- Try to avoid long and wide tables , The field does not exceed 50 individual
- Add comments to tables and fields
- It is forbidden to store images in the database , Big data such as files , It is suggested to put it in oss The server
- Naming specification
- Lowercase letters , And use underline segmentation
- Temporary storage , Table tmp The prefix , And suffixed with date , for example tmp_table_20220104
- Stored procedures are not recommended 、 trigger 、 View etc.
- Business
- Atomicity A
- Uniformity C
- Isolation, I
- persistence D
- Transaction isolation level
- RU Read uncommitted 【 Dirty reading allowed 】
- RC Read submitted 【 It can't be read repeatedly 】
- RR Repeatable degrees 【 Fantasy reading 】
- S Serialization
- Field design specification
- Follow less but better
- Try to choose the smallest data type
- Appropriate redundancy
- All fields need to be added not null attribute
- Use unsigned Store nonnegative values
- add to unsigned attribute , You can use the same space , Double the data storage range , for example int type (-2147483648,2147483647), And add unsigned after , The value range is (0,4294967295)
- Use int Store ip value
- inet_ntoa() and inet_aton Can achieve ip And decimal numbers
- Use as small as possible varchar Field
- Don't arbitrarily convert types between fields
- Fields need to have default values
- Control index 、 Contains the number of fields
- No redundancy 、 Duplicate index
- No indexing null value
- Foreign keys are not allowed
- Reasonably use index coverage to avoid io
- Update and disassemble in large quantities to reduce the particle size
- sql Design
- use in Instead of or
- Implicit type conversions are prohibited — Will invalidate the index
- Avoid using join And subquery
- Subqueries and more than 3 Above table join It is very easy to cause slow sql And affect mysql Overall performance of
- To avoid the mysql The index column of is used for mathematical and functional operations
- Avoid big business
- data , It is recommended to obtain data in batches , Less data is obtained each time 500 strip , Result set should be less than 1M
- use union all Instead of union
- union Will remove duplicate data , And sort it out
- union all Will ignore sorting and de duplication , Merge the results directly , Compared with sorting, it saves a lot of computing resources
- Avoid full table scanning
- Do not use leading % Go to query
- A separate slave library can be used for similar query operations
- No right null Make relevant judgments
- Splicing sql Pay attention to prevent injection
- update and delete belt where Conditions
Redis
- Naming specification
- Use standard
- Capacity ,qps forecast
- A single node qps stay 2w within
- Structure selection
- String
- k|v
- Hash
- Set
- Sorted Set
- List
- String
- Use standard
- Separation of hot and cold data
- Different business data are stored separately
- Storage key Be sure to set the timeout
- The stored large text data must be compressed first and then stored , Do not store pictures , Video etc.
- on-line redis Do not use dangerous commands
- hmget
- hgetall
- hvals
- hkeys
- smembers
- keys
- …
- When using lists as queues , Monitor the queue length
troubleshooting
- Complete table scanning without cable guidance
- The use of functions on index columns leads to index invalidation
- Implicit type conversion of data type leads to index invalidation
- Use union all Instead of or【 Demolition sql sentence , You can use index 】
Master slave database building 【docker】
Pull the mirror image
docker pull mysql:5.7.37
docker run Create the main container
docker run \ -p 3307:3306 \ --name=mysql-master \ -v /mydata/mysql-master/log:/var/log/mysql \ -v /mydata/mysql-master/data:/var/lib/mysql \ -v /mydata/mysql-master/conf:/etc/mysql \ -e MYSQL_ROOT_PASSWORD=root \ -d mysql:5.7.37
docker ps Check operation
stay /mydata/mysql-master/conf/ Create under folder my.cnf Profile file
[mysqld] ## Set up server-id, The same LAN is globally unique server-id=101 ## Specify the name of the database that does not need to be synchronized binlog-ignore-db=mysql ## Turn on binary log function log-bin=mall-mysql-bin ## Set the memory size of binary log ( Business ) binlog_cache_size=1M ## Set the log format using binary (mixed,statement,row) binlog-format=mixed ## Binary log expiration cleanup time , The default value is 0, Does not automatically clean up expire_logs_days=7 ## If all errors encountered in master-slave replication or specified types of errors , avoid slave End copy interrupt ## Such as :1062 An error is a duplicate of some primary keys ,1032 The error is because the master-slave database is inconsistent slave_skip_errors=1062
After creating the above configuration file , Restart the container instance
Into the container , And in maser Create a data synchronization user inside the container instance
CREATE USER 'slave'@'%' IDENTIFIED BY '123456'; GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'slave'@'%';
docker run Create from container
docker run \ -p 3308:3306 \ --name=mysql-slave \ -v /mydata/mysql-slave/log:/var/log/mysql \ -v /mydata/mysql-slave/data:/var/lib/mysql \ -v /mydata/mysql-slave/conf:/etc/mysql \ -e MYSQL_ROOT_PASSWORD=root \ -d mysql:5.7.37
docker ps Check operation
stay /mydata/mysql-slave/conf/ Create under folder my.cnf Profile file
[mysqld] ## Set up server-id, The same LAN is globally unique server-id=102 ## Specify the name of the database that does not need to be synchronized binlog-ignore-db=mysql ## Turn on binary log function , in preparation for slave1 As an instance of other databases master When using log-bin=mall-mysql-slave1-bin ## Set the memory size of binary log ( Business ) binlog_cache_size=1M ## Set the log format using binary (mixed,statement,row) binlog-format=mixed ## Binary log expiration cleanup time , The default value is 0, Does not automatically clean up expire_logs_days=7 ## If all errors encountered in master-slave replication or specified types of errors , avoid slave End copy interrupt ## Such as :1062 An error is a duplicate of some primary keys ,1032 The error is because the master-slave database is inconsistent slave_skip_errors=1062 ## relay_log Configure relay logs relay_log=mall-mysql-relay-bin ## log_slave_updates Express slave Write the copy event to your own binary log log_slave_updates=1 ## slave Set to read only ( have super Privileged user ) read_only=1
Restart from container , And enter the container to view
mysql -uroot -p; root
Execute the following statement :
change master to master_host=' host ip',master_user='slave',master_password='123456',master_port='3307',master_log_file='mall-mysql-bin.000001',mysql_log_pos=617,mysql_connect_retry=30;
Parameter description master_host Master database ip Address master_user Create a user account for synchronizing data in the master database master_password Create a user password in the master database for synchronizing data master_port Main database running port master_log_file Specify the log file for copying data from the database , By checking the master data of Zhuang Yi , obtain File Parameters mysql_log_pos Specify where to start copying data from the database , By checking the master data status , obtain position Parameters mysql_connect_retry The connection failed and the time interval between retries , Company s
binlog Open Settings
win10
- view the database binlog Whether to open
show variables like 'log_bin';
show variables like 'log_bin%';
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-2aqv85da-1644390879899)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20210616101339651.png)]
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-c4QNpv2M-1644390879901)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20210616112031907.png)]
open binlog journal
2.1 find mysql Configuration file for mysql.ini【 stay mysql Installation directory 】
2.2 stay mysqld The configuration is as follows
# Turn on binlog journal server_id=1 log-bin=mysql-bin binlog-format=ROW
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-m7xZ8hsl-1644390879903)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20210616112246377.png)]
binlog-format: There are three forms of master-slave synchronization
statement Will operate on the database sql The statement is written to binlog in
row Every data change will be written to binlog in .
mixed statement And row Mixing .Mysql Decide when to write statement Format , When to write row Format binlog
2.3 Restart the service , Check again
net stop mysql net start mysql
show variables like 'log_bin%';
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-P9FWI9EW-1644390879903)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20210616112407603.png)]
linux
- see binlog state
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-1CVTzz6S-1644390879904)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20210616113128885.png)]
Locate the configuration file and modify it
The location of the configuration file
/etc/my.cnf
Add the configuration
# Turn on binary log server-id=1 log-bin=/usr/local/mysql/data/mysql-bin
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-4fD6aWD3-1644390879905)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20210616113847798.png)]
docker
The configuration is mapped to a local file , The local file configuration is as follows
[mysqld] #binlog setting log-bin=/var/lib/mysql/mysql-bin server-id=123454 binlog-format=ROW
restart mysql, Search found binlog It's not turned on
docker restart mysql
It took a long time to find this hole on the Internet , Record the process
First , see docker About my.cnf I found this sentence in my log :
mysqld: [Warning] World-writable config file '/etc/mysql/``my.cnf``' is ignored.
Viewing on the Internet is caused by the permission of the configuration file https://blog.csdn.net/qilovehua/article/details/45508925, For details, please refer to this blog post
Then enter docker Of mysql Container to check , Sure enough my.cnf The permissions for are 777
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-LPOhkoXk-1644390879905)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20210616142116847.png)]
Change it to 644
chmod 644 my.cnf
Restart again mysql Containers , Check it out binlog Successfully opened
however , If you make any changes to the mapped configuration file later , He will be mapped again to /etc/mysql/my.cnf This file and the permissions are reset to 777, Need to modify again
canal Use research
canal-server
https://github.com/alibaba/canal/releases/download/canal-1.1.5/canal.deployer-1.1.5.tar.gz
canal-adapter
https://github.com/alibaba/canal/releases/download/canal-1.1.5/canal.adapter-1.1.5.tar.gz
Database synchronization command
/opt/zbox/run/mysql/mysqldump -uroot -pZenTao-Cestc123 --databases zentaoep | /opt/zbox/run/mysql/mysql -h 10.255.237.120 -uroot -pwhut.VINCENT920812
/opt/zbox/run/mysql/mysqldump -uroot -pZenTao-Cestc123 --databases zentaoep | /opt/zbox/run/mysql/mysql -h 10.101.42.163 -uroot -proot
/opt/zbox/run/mysql/mysqldump -uroot -pZenTao-Cestc123 --lock-tables=0 zentaoep > /opt/zbox/mysql_bak_`date +%Y%m%d`.sql
边栏推荐
- Leetcode problem solving summary, constantly updating!
- Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
- 1. Somme des deux nombres
- Apt update and apt upgrade commands - what is the difference?
- Convolution operation in convolution neural network CNN
- Leetcode solution - 01 Two Sum
- phpstudy设置项目可以由局域网的其他电脑可以访问
- Loss function in pytorch multi classification
- Project summary --04
- Simple understanding of ThreadLocal
猜你喜欢
tabbar的设置
phpstudy设置项目可以由局域网的其他电脑可以访问
Skywalking8.7 source code analysis (I): agent startup process, agent configuration loading process, custom class loader agentclassloader, plug-in definition system, plug-in loading
輕松上手Fluentd,結合 Rainbond 插件市場,日志收集更快捷
[teacher Zhao Yuqiang] MySQL high availability architecture: MHA
Oauth2.0 - user defined mode authorization - SMS verification code login
[teacher Zhao Yuqiang] the most detailed introduction to PostgreSQL architecture in history
Kubernetes notes (IX) kubernetes application encapsulation and expansion
Zhiniu stock project -- 04
[teacher Zhao Yuqiang] index in mongodb (Part 2)
随机推荐
最大似然估计,散度,交叉熵
Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
pytorch 搭建神经网络最简版
[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence
Exportation et importation de tables de bibliothèque avec binaires MySQL
Understand expectations (mean / estimate) and variances
Fluentd facile à utiliser avec le marché des plug - ins rainbond pour une collecte de journaux plus rapide
Alibaba cloud Alipay sandbox payment
Redhat7 system root user password cracking
When PHP uses env to obtain file parameters, it gets strings
ThreadLocal的简单理解
Kubernetes notes (V) configuration management
Zhiniu stock project -- 04
Kubernetes notes (IV) kubernetes network
Code generator - single table query crud - generator
项目总结--04
Cesium 点击获三维坐标(经纬度高程)
Complete set of C language file operation functions (super detailed)
Disruptor learning notes: basic use, core concepts and principles
深入解析kubernetes controller-runtime