在MySQL服务器启动时,它检查其命令行的操作,来查看它是否应该执行登录并打开相应的日志文件(如果应该的话)。可以让服务器生成两种主要类型的日志文件:常规日志文件。它报告客户机的连接、查询和其他各种各样的事件。它对于跟踪服务器的活动很有用:谁正
在MySQL服务器启动时,它检查其命令行的操作,来查看它是否应该执行登录并打开相应的日志文件(如果应该的话)。可以让服务器生成两种主要类型的日志文件:常规日志文件。它报告客户机的连接、查询和其他各种各样的事件。它对于跟踪服务器的活动很有用:谁正在连接、从哪里连接,以及他们正在做什么。
更新日志
它报告修改数据库的查询。在此上下文中的术语“更新”不只涉及UPDATE语句,还涉及修改数据库的所有语句。由于这个原因,它包含了对D E L E T E、INSERT、REPLACE、CREATE TABLE、DROP TABLE、GRANT 和REVOKE 的查询记录。更新日志的内容以SQL 语句的形式书写,这些语句用作对mysql的输入。如果在崩溃后必须恢复表的话,更新日志与备份是很有用的。您可以从备份文件中恢复数据库,然后通过将更新日志作为对mysql的输入,重新运行在该备份文件之后又修改数据库的任何查询。这样,可将表恢复到崩溃时刻的状态。
为了使日志有效,可使用--log 选项开启常规日志,并用--log-update 选项开启更新日志。可以在mysqld.safe_mysqld 或mysql.server 的命令行中,或在某个选项的[mysqld] 组中指定这些选项。当日志有效时,日志文件在缺省时被写到服务器的数据目录中。
笔者建议在首次使用MySQL时应使两种日志类型都有效。在获得一些使用MySQL的经验后,可能会只用更新日志来对付,以便减少磁盘空间的需求。
在使日志有效后,要确保不用大量的日志信息将磁盘填满,尤其是如果服务器正在处理大量的查询话。可使用日志文件循环和截止时间,在避免日志文件无边界地增长的同时保持最近的几个日志是联机可用的。
日志文件循环工作如下。假定日志文件名为l o g。在第一个循环中, log 被重新命名为l o g . 0,且服务器开始写新的l o g文件。在第二次循环中, log.0 被重命名为l o g . 1,log 重命名为l o g . 0,服务器开始写另一个新的log 文件。这样,每个文件循环通过名字l o g . 0、l o g . 1,等等。当文件到达循环的某一点时,可以终止它。
更新日志和LOAD DATA 语句
通常,当服务器执行LOAD DATE 语句时,它只将该语句本身而不是被加载的行内容写到更新日志中。这意味着除非该数据文件仍然保持可访问,否则使用更新日志的恢复操作将是不完整的。为了确保这一点的安全,除非数据库已经备份,否则不应该删除数据文件。
系统备份
更新日志对于数据库恢复并不是任何时候都好,如果一个磁盘崩溃导致您失去了更新日志的话,应确保您执行定期的文件系统备份。将更新日志写到与存储数据库不相同的磁盘中也是一个好主意。
例如,如果您每天都循环日志,并且想保持一周的日志,则应保留log.0 到l o g . 6。在下一个循环中,将通过令log.5 覆盖log.6 使其成为新的log.6 来终止l o g . 6。这样,您就可以保留许多日志而又避免了它们超过磁盘的限度。
日志循环频率和保持的旧日志数量将依赖于服务器的繁忙程度(活动的服务器产生更多的日志信息)以及您希望为旧日志投入多少磁盘空间。当循环常规日志时,可以用mysqla d - min flush-logs 命令告诉服务器关闭当前的日志文件并打开新的日志文件。
执行常规日志循环的脚本类似如下(可修改它来反映您的日志基名和数据目录的位置,或许还有希望保留的旧日志的数量):
最好从mysqladm 账号中运行此脚本以确保日志文件属于那个用户。如果在.my.cnf 选项文件中保留连接参数,您不需要在该脚本的mysqladmin 命令中指定任何参数。如果您不这样做的话可以建立一个受限用户,它除了发布刷新命令外什么也不做。然后可以以最小的风险在该脚本中放置这个用户的口令。如果想这样做,则该用户应只有RELOAD 权限。例如,要想调用用户flush 并分配一个口令f l us h pass,可使用下列GRANT 语句:
GRANT RELOAD ON *.* TO flush@localhost IDENTIFIEDBY "flushpass"
当需要在脚本中执行刷新操作时,可以这样做:
mysqladmin -uflush -pflushpass flush -logs
在Linux 中,最好用logrotate 来安装MySQL分发包中的mysql- log - rotate 脚本,而不是自己编写脚本。如果mysql-log-rotate 不通过RPM 文件自动安装,应查看MySQL分发包的support-files 目录。
由于服务器处理更新日志文件的方法不同,日志文件的循环在更新日志与常规日志之间稍有不同。如果告诉服务器使用没有扩展名的更新日志文件名(如up date),则服务器将使用顺序的up date . 0 0 1、update.002 等自动创建更新日志文件名。在服务器启动以及在日志刷新时,一个新的更新日志产生。如果您开启更新日志而没有指定文件名,服务器则使用主机名作为基名产生一个更新日志文件的序列。
当终止一个由这种方法生成的文件序列时,您或许想要根据其期限(最后被修改的时间)而非根据名字来终止它们。这样做的理由是由于您不知道flush-log 命令将在何时发布,因此您不能指望在任何给定的时间周期内创建固定数量的更新日志。例如,如果用mysqldump 备份表并使用--flush-logs 选项,在该更新日志名序列中的一个新文件随每个备份一同创建。
对于带有由服务器自动产生的顺序文件名的更新日志,基于日志期限的终止脚本类似如下:
find 命令定位并删除修改时间超过一个星期的更新日志文件。重要的是使用-name 参数来对一个数字的文件扩展名进行测试,以避免删除由错误的update 所指定的表。
还可以告诉服务器使用固定的更新日志文件名(如果希望的话),如果想用与常规日志相同的方法循环更新日志,这是有用的。要想使用固定的更新日志名,应指定一个包含扩展名的名字。例如,可以用--log-update=update.log 选项启动服务器来使用名字up date . l o g。服务器将一直关闭并在接收flush-logs 命令时打开该日志,但是服务器并不是每次都产生新的文件。在这种情况下,用于更新日志的日志循环脚本和用于常规日志的脚本仅在循环的文件基名上有所不同。
如果想自动执行日志循环和终止,可使用c r o n。假定循环常规日志和更新日志的脚本为rotate-logs 和r o t a t e - up date - l o g s,且安装在/usr/user/mysql/bin 目录中。以mysqlladm 用户进行注册,然后用以下命令编辑mysqladm 用户的crontab 文件: % crontab -e
此命令允许编辑当前crontab 文件的备份(如果之前没有这样做,则它可能为空)。按以下方法将行增加到该文件中:
这个项告诉cron 在每天早上4 点运行此脚本。您可以改变时间或按需要进行调度。有关说明请参见crontab 的人工页。

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment