当前位置:网站首页>MySQL development practice summary (I)
MySQL development practice summary (I)
2022-06-09 23:59:00 【The simplicity of the road】
1、 Determine the version online
①、 The current mainstream is 5.7.X and 8.X edition ,8.x Version optimization , Better read / write performance .
2、 Installation mode :
①、 The simplest test uses , Download a package directly from the official website , Follow the tutorial to install the configuration , have access to Docker To install and use , It is also faster and more convenient , Open the port after deployment on the test machine , The connection test can be used .
②、 Production use : The simplest active / standby or read / write separation libraries can be built on different machines , And configure some monitoring scripts , Hang the tray separately SSD Keep it stable ,binlog Specify the corresponding save time expiration cleanup , You can also buy cloud services RDS database , Select the corresponding instance specification , With complete monitoring , Migrating data , Expansion specification , Operation and maintenance are quite convenient , Optimization parameters , General enterprises are using , Business uses direct connections vip Floating address , Can be safe .
③、 Disaster recovery transformation : Ensure highly available disaster recovery architecture for databases , Cloud services are generally purchased in the same Region Two machines , Large scale project guarantee disaster recovery architecture , Usually in different Region There are also examples of transformation , Span AZ disaster , One main, one standby and one cascade , For example, both Beijing No. 4 and Beijing No. 1 have , Then give a float IP, Even on one side AZ Hanging up does not affect business stability .
3、 Library table permissions
①、 General maximum authority Root All are DBA be responsible for , For some businesses, create libraries and tables and separately assign certain reasonable and controllable permissions ,grant Grant permissions to tables , For some, the timer and partition table permissions are allocated separately , The master is copied to a separate designated account , You can specify a network segment and specify IP To achieve replication , Prevent operation with too large permission .
②、 For temporary business needs , You can specify a temporary account number , Then you can delete it after you use it , It is applicable to modifying business database passwords and special businesses .
③、 port 、 password 、 Account compliance , No default 3306, Pay attention to network segment VPC Divide .
4、 Build a table to regulate
①、 Build table SQL Code warehouse archiving , The create table field specifies the description , The primary key depends on the business scenario , Select auto increment 、UUID、 Joint multi field primary key , although innodb A self incrementing primary key can specify its location , The index nodes can be connected in ascending order ,B+ The order of the number of inodes is always , However, it is not recommended if there are too many modifications and deletions , Design according to the actual scene , The following tables are also troublesome .
②、 Properly evaluate business scenarios and build appropriate indexes , Useless indexes will cause the write data to be indexed too slowly, which will affect the speed , Joint indexing is preferred , Override index is preferred .
③、 Evaluate useless indexes , Delete if you can , No waste of necessary storage space and write speed .
④、 Create a table to specify the data creation time, update time, and logical deletion of some business scenarios .
5、 Data cleaning
①、 General business data will be saved for a certain period of time , Delete data according to the duration , Remember to defragment the tablespace , Generally delete data 、 There will be some fragments in the updated data , Cannot fully utilize , Lead to mysql When writing data, there is no full page space , Waste space .
②、 Time series data can consider partition table , Such as according to the time Range The way of division , By day or by month , The table is transformed into a partition table , Pre build future tables , Regularly delete the previous partition table , Can completely solve the problem of table space expansion , Based on distributed timed task time , For deletion failure or partition establishment failure, service alarms can be sent to meet the stability , Use es It is also a way of dividing by day or month .
③、 It can be implemented based on certain rules , And then directly after the expiration drop Physical tables are also more convenient , Every day and every month , In business, it is more convenient to have a vertical database .
④、 Delete library table data drop、 Delete some data delete、 Clear the table data directly truncate.
⑤、 You can also use mysql Of event Timer , But it's not controllable , In case of abnormal conditions, the priority cannot be given .
⑥、 The data backup , have access to xxl-job Of shell Execute the backup script regularly , To different components , If OBS Bucket or some object storage medium , have access to Flink CDC2.0 To synchronize to other databases .
6、 Sub database and sub table
①、 Vertical sub database : According to different business Split different database Easy to expand , Different database instances can be deployed Effectively reduce database pressure .
②、 Vertical sub table : Split large table fields 、 Split into small tables 、 Reduce single table fields .
③、 Horizontal sub table : according to The data field module is used to split different databases and tables , Make single table naming rules , If different types are spliced to form a complete table , Then the business side reads and writes the specified table , Yes hash modulus , For the specified account number %20, Here take 20 Zhang Zibiao , Then it's divided into 0-19, The data range is estimated by table , Then write to different libraries , Of course, you can hash Take the mold and divide the warehouse , Then divide the sub table in the range .
④、 Data migration can use some vendors' cloud services , Or double write migration , Interface connects different databases grafana Monitor and observe .
7、 Business time storage
①、 Use it directly point Time stamp in seconds of 、 Millisecond time , With long Type storage .
②、datetime String mode .
8、 Necessary parameter tuning
①、 For example, the maximum number of connections innodb-buffer-pool-size、max-connections、log-bin Maximum time 、 Maximum packet size 、 Server character set and characters , The maximum number of connections is evaluated according to the number of database instance connections , Generally, the number of business parties' database connection pools and cluster specifications are reasonably set .
②、 Specify the longest life cycle for the corresponding database connection pool , Expired release , Then create a new , To a certain extent, reduce the server connection memory surge scenario , The database connection pool connects with a certain monitoring platform to collect data , Observe the situation and adjust .
③、 Error log 、 Binary log 、 Relay log configuration .
9、 Database driven selection
①、 General choice mysql drive ,8.x Driver compatibility 5.7.x Database version .
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency><dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>②、 consider License Safe to use , yes mysql An open source branch of .
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.0.5</version>
</dependency>10、 Database time and encoding and opening SSL
①、 Database time zone view :show variables like '%time_zone%';
②、 Set up set time_zone = '+8:00';
③、 View the database code :show variables like "char%";
④、 No configuration by default SSL Certificate location , Use in the drive useSsl=false, Configure the certificate to enable ;
11、ORM Frame selection
①、MyBatis、MyBatisPlus Single table multi table dynamic SQL, It is suggested to encapsulate , It is a big project to change and upgrade .
②、Hibernate encapsulation BaseDao, Other businesses develop their own XXDao, Bottom layer to use BaseDao Business methods in , Follow up design to upgrade the open source package , Change the corresponding API The smallest is the smallest , It's more convenient , It is also convenient to maintain the platform layer .
③、JdbcTemplate/Jdbc To complete the original precompile SQL.
12、 monitor 、 Deadlock 、 Business 、 Connect
①、information_schema library

②、sys library

Positioning lock waiting
show processlist【 Check the connection Query】
①、select * from information_schema.innodb_lock_waits;
Positioning lock
①、select * from information_schema.innodb_locks;
Locate transactions
①、select trx_id,trx_started,trx_requested_lock_id,trx_query,trx_mysql_thread_id from information_schema.innodb_trx;
The result is two transactions ,MySQL Transaction thread id by 38 and 41, Very intuitive to see 41 It is our delete Business , By 38 lock
Locate threads
①、select * from performance_schema.threads where processlist_id=38;
It turns out that MySQL Transaction thread 38 Corresponding server thread 63.
Positioning and locking SQL
select * from performance_schema.events_statements_current where thread_id=63;
You can query relevant information and analyze problems from these databases .
I hope to be right mysql There is a clear knowledge network in the development process , Constantly improve the knowledge system , Provide solutions that are more suitable for business scenarios .
边栏推荐
- “當你不再是程序員,很多事會脫離掌控”—— 對話全球最大獨立開源公司SUSE CTO
- 香橙派H3烧录Uboot,远程加载zImage,dtb,rootfs
- 单细胞数据复现-肺癌文章代码复现7
- 2022年金属非金属矿山爆破考试题库及答案
- Recommendation letter | look at the sky step by step, hand in hand, and see the stars one by two
- Deploy MySQL based on statefulset in kubernetes (Part 1)
- Introduction to C # WPF layout control layoutcontrol
- Install idea
- Online JSON to CSV tool
- 企业无忧 | Apipost私有化部署活动即将火热开启
猜你喜欢

荐书 | 手牵手一步两步望着天,看星星一颗两颗连成线

Microcomputer principle and interface technology exercise 1

模型部署简述

低边驱动和高边驱动

Manual single precision floating point type

Anatomy of illusory rendering system (15) - XR topic

2022爱分析· 隐私计算厂商全景报告 | 爱分析报告

模拟退火-n皇后问题

"Leak detection and vacancy filling" Android internship interview knowledge points (II)

Book club recruits | let's read "Mr. toad goes to see a psychologist"
随机推荐
你了解单例模式吗?反正我不了解。
C # WPF generates table with variable rows and columns from background code
Install idea
Record the 'new' course of an emergency investigation
sparksql源码系列 | 一文搞懂Partitioning源码体系(spark3.2)
动态读取protobuf数据
How can C get entity class attribute names and values?
没有项目管理经验,可以参加PMP考试?
Anatomy of illusory rendering system (15) - XR topic
478. 在圆内随机生成点
Introduction to C # WPF layout control layoutcontrol
poi 导出excel实战
单细胞数据复现-肺癌文章代码复现7
C# WPF后台动态添加控件(经典)
CSRF/XSRF简介
C# WPF从后台代码生成行列可变的表格
Manual single precision floating point type
查看本机电脑wheel可安装版本号
2022 operation of simulated examination platform for examination question bank of pillar special operation certificate for metal and nonmetal mines
I've taken it. The MySQL table has 500W rows. Is there anyone who doesn't partition?