Open source database Mysql_DBA operation and maintenance practice (summary)

Open source database Mysql_DBA operation and maintenance practice (summary)

What types of SQL statements contain

DDL

DCL

DML

DQL

Yum installs MySQL configuration files

  1. Configuration file: /etc/my.cnf
  2. Log directory: /var/log/mysqld.log
  3. Error log: /var/log/mysql/error.log

MySQL master-slave switch

  1. View master-slave replication status
  2. Stop write operations on the primary database
  3. Record the current secondary log file and location
  4. Update configuration from database
  5. Enable write operations on the new primary database

Steps of MySQL master-slave replication

MySQL master-slave replication is a mechanism for data backup and load balancing that allows write operations to be performed on one MySQL server (master server) and synchronized to one or more other MySQL servers (slave servers). In this way, database performance, reliability, and scalability can be improved.

The basic steps required for MySQL master-slave replication:

1. Configure the master server
   On the master server, you need to enable binary logging (binlog) and set a unique server ID. Binary logging will capture all writes to the database, while the server ID will provide a unique identifier between the master and slave.

2. Configure the slave server
   On the slave server, you need to enable relay logging (relay log) and specify it as the binary log location of the master server. Then set a unique server ID for proper data synchronization between master and slave.

3. Initiate the replication process
   On the master server, a slave server account must be created and granted replication permissions. The slave will use this account to connect to the master and check the binary log for changes. On the slave server, connection information must be configured to ensure proper communication with the master server.

After completing these steps, any changes made on the master server will be automatically synced to the slave server. The slave server will be a read replica, which can be used to handle read-only queries, while the master server handles write operations.

It should be noted that in MySQL master-slave replication, the slave server can only read data. If any write operations are to be performed on the slave, it must be started as the master, and it must be ensured that data synchronization is properly handled when switching over.

The principle of MySQL master-slave replication

The premise is that the database server in the role of the primary server must enable the binary (binlog) log

Principle: Any modification on the main server will be saved in the binary log (Bin-log log). Start an I/O thread from the server, connect to the main server to request to read the binary (Bin-log) log, and then write the read binary log to the local Realy-log (relay log). Start a SQL thread from the server at the same time, read the Realy-log (relay log), if any update is found, immediately execute the updated content on the local database.

How to solve MySQL master-slave delay

  1. Network Optimization: Latency issues can be related to network bandwidth and latency. Ensuring a stable network connection between the master and slave servers and optimizing network bandwidth can reduce replication delays.

  2. Optimize replication thread: In MySQL master-slave replication, one I/O thread is responsible for reading the binary log of the master server, and another SQL thread is responsible for applying the log to the slave server. Optimizing the performance parameters and configuration of these replication threads, such as increasing the number of threads and adjusting the buffer size, can improve the efficiency of replication.

  3. Reduce the load on the master server: If the load on the master server is high, it may cause replication delays. Consider optimizing the performance and load of the master server, such as adjusting query optimization and increasing hardware resources, to reduce the load on the master server and improve replication performance.

  4. Optimizing the configuration of slave servers: The performance and configuration of slave servers can also affect replication latency. Ensure that the slave server has sufficient resources, such as CPU, memory, disk, etc., and perform appropriate configuration optimization according to the actual situation to improve replication performance.

  5. Monitoring and tuning: Regularly monitor the delay of master-slave replication and analyze the cause of the delay. Perform tuning based on monitoring results, such as adjusting replication parameters, optimizing query statements, increasing hardware resources, etc., to reduce replication delay.

MySQL optimization operation

MySQL is a commonly used relational database management system. Optimizing it can improve the performance and efficiency of the database.

We can regularly optimize and rebuild indexes to improve the performance and efficiency of indexes, use appropriate query caching mechanisms, adjust MySQL configuration parameters according to load conditions, design table structures reasonably, avoid redundant data, and use master-slave replication and master-slave clusters to Realize read-write separation and load balancing to improve system availability and performance

The following are some common MySQL optimization operations:

1. Index optimization: Speed ​​up queries by creating appropriate indexes. You can use the `CREATE INDEX` statement to create an index, or you can use the `EXPLAIN` command to analyze the execution plan of the query statement and find out the fields that need to be indexed.

2. Query optimization: Avoid using `SELECT *` to query all fields, but only select the required fields. Use the `WHERE` clause to limit the scope of the query to avoid full table scans. Use the `JOIN` statement to optimize multi-table queries.

3. Configuration optimization: adjust MySQL configuration parameters to meet actual needs. For example, increasing the value of the `innodb_buffer_pool_size` parameter can improve the performance of the InnoDB storage engine.

4. Table structure optimization: Avoid using too many redundant fields and tables, and design the table structure reasonably. Use appropriate data types to store data to avoid wasting space.

5. Cache optimization: Use cache to reduce access to the database. You can use the query cache function that comes with MySQL, or you can use an external cache system such as Redis.

6. Use of query cache: MySQL's built-in query cache function can cache query results and improve query performance. However, in a high-concurrency environment, query caching may cause performance problems, so it is necessary to decide whether to enable query caching according to the actual situation.

The above are just some common MySQL optimization operations, and the specific optimization strategy needs to be adjusted according to the actual situation. Before performing optimization operations, it is recommended to perform performance testing and analysis to find out where the bottleneck is, and select an appropriate optimization method based on the actual situation.

1. Choose an appropriate storage engine: InnoDB
2. Choose a device with a faster disk read and write speed as the Mysql server
3. Make full use of the index
4. Add the slow query log function
5. Use the cache database to solve frequently accessed data
6. Single query Finally, increase LIMIT 1 to stop the full table scan
7. Set the maximum number of connections
8. Set the read and write speed of the engine

How to back up MySQL

Use the mysqldump command to export the structure and data of the mysql database and save it as a sql file

Mysqldump -u username -p password --all-databases > backup.sql

MySQL cluster architecture

cluster purpose

Load balancing to solve high concurrency

High availability HA service availability

Data availability for remote disaster recovery

one master one slave

Master node: Responsible for accepting client write requests and synchronizing written data to the node

Slave node: can only read data, but cannot write to the database. Through asynchronous replication, it accepts the binlog of the master node and replays these operations locally to achieve data synchronization with the master database.

MySQL read and write separation

Definition: MySQL read-write separation refers to the allocation of database read and write operations to different servers for execution to improve the overall performance of the server.

Implementation method: Generally, MySQL read-write separation can be achieved by manually writing read-write separation code in the application program or using third-party middleware to add a proxy layer between the application program and the database. At present, the more commonly used middleware are MHA, MaxScale, proxysql, etc.

Principle: The principle of read-write separation is to concentrate all write operations on one master server, and distribute read operations to multiple slave servers, and ensure data consistency through replication. The advantage of read-write separation is that it can avoid the impact of too many query operations on the main server, thereby improving the concurrency of the application.

Configuration: When configuring MySQL's read-write separation, you need to specify the address and port number of the read-write separation in the application, and pay attention to whether the read operation will be abnormal or not. Maintenance: When maintaining the read-write separation of MySQL, both the master server and the slave server need to be monitored to minimize latency and ensure data consistency. At the same time, you need to do a good job of backup and recovery so that you can recover quickly in the event of a failure.

The specific recovery process of MySQL

If there is a backup file, you can restore it from the backup file, stop mysql, copy the backup file to the mysql directory, and start mysql

Guess you like

Origin blog.csdn.net/SongLiang02_/article/details/132361002