Home Database Mysql Tutorial 每个 MySQL 开发者都应该了解的 10 个技巧

每个 MySQL 开发者都应该了解的 10 个技巧

Jun 07, 2016 pm 05:31 PM

MySQL是世界上实际最流行的数据库管理系统,是遍布全球编程社区的首

MySQL是世界上实际最流行的数据库管理系统,是遍布全球编程社区的首选。它有一个系列有趣的特性,在很多方面都很擅长。由于其巨大的人气,在网上可以找到许多MySQL的使用技巧。这里有12个最好的技巧和窍门,所有MySQL数据库开发者都应该了解一下。

mysql

避免编辑转储文件 MyISAM 块大小

大多数开发者忘记了这一事实,文件系统往往需要一个大的MyISAM块以保证高效运行。许多开发者不知道块大小的设置。.MYI文件存储在myisam_block_size的设置里,这个设置项可用来修改大的块尺寸。MyISAM块大小的默认值是1K,这不是当前大多数系统的恰当设置。因此,开发者应该考虑指定一个与之相适应的值。

打开 Delay_Key_Write

为避免系统崩溃时数据库损坏delay_key_write默认是关闭的。有人可能会问,如果是这样的话,为什么要把它放在首位打开呢?从防止数据库每次写MyISAM文件时刷新密钥文件方面看这是必要的。通过把它打开,开发者可以节省很多时间。参考MySQL官方手册了解你的版本如何把它打开。

Joins(表连接)

创建索引和使用相同的列类型:join(表连接)操作可以在Mysql中被优化。若应用中有许多join操作,可以通过创建相同的列类型上join来优化。创建索引是加速应用的另一种方法。查询修改有助于你找回期望的查询结果。

优化WHERE从句

即使你只搜索一行MySQL也会查询整个表,因此,建议你当只需要一条结果时将limit设置为1。通过这样做,可以避免系统贯穿搜索整个表,从而可以尽可能快找到与你需求相匹配的记录。

在Select查询上使用Explain关键字

你肯定希望得到与任何特定查询相关的一些帮助。Explain关键词在这方面是非常有帮助的。它在你寻求查询到底做了什么时提供了具体细节。例如,在复杂join查询前键入Explain关键词你会得到很多有用的资料。

使用查询缓存优化查询

MySQL的查询缓存是默认启用的。这主要是因为缓存有助于查询的快速执行,,缓存可以在相同的查询多次运行使用。你在关键字前加入当前日期、CURRDATE等PHP代码使查询缓存它从而启用此功能。

使用堆栈跟踪隔离Bug

各种Bug可以使用stack_trace隔离出来。一个空指针足以毁掉一段特定的代码,任何开发人员都知道它有这样的能力。了解使用堆栈跟踪的细节,从而在你的代码里避免bug。

设置SQL_MODE

枚举类型总是让人感到非常的疑惑。由于字段可能拥有多个可能的值,这些可能的值包括你指定的和null,在编码时将会出现很多问题,你将永远都会得到一个警告说代码不正确。一个简单的解决办法就是设置SQL_MODE。

//Start mysqld with
$–sql-mode=”modes”
//or
$sql-mode=”modes” (my.ini – Windows / my.cnf – Unix)
//Change at runtime, separate multiple modes with a comma
$set [GLOBAL|SESSION] sql_mode=’modes’
//TRADITIONAL is equivalent to the following modes:
STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, ERROR_FOR_DIVISION_BY_ZERO, and NO_AUTO_CREATE_USER

修改Root密码

修改root密码对于某些特定设置是必不可少的,修改命令如下:

//Straightforward MySQL 101
$mysqladmin -u root password [Type in selected password]
//Changing users ROOT password
$mysqladmin -u root -p [type old password] newpass [hit enter and type new password. Press enter]
//Use mysql sql command
$mysql -u root -p
//prompt “mysql>” pops up. Enter:
$use mysql;
//Enter user name you want to change password for
$update user set password=PASSWORD (Type new Password Here) where User = ‘username’;
//Don’t forget the previous semicolon, now reload the settings for the users privileges
$flush privileges;
$quit

用MySQL Dump 命令备份数据库

开发者都知道数据库备份的重要性,当系统出现重大故障时能够起到救命的作用。

最简单的备份数据库的方法

1 $mysqldump –user [user name] –password=[password] [database name] > [dump file]

//你也可以用简写"-u","-p"来分别代替"user"和"password"
//将多个数据库导入到一个文件只要在后面添加需要导出数据库的名称:

 

1 mysqldump –user [user name] –password=[password]

2 [first database name] [second database name] > [dump file]

//许多数据库都提供了顺序备份的功能,要备份所有数据库只需要添加--all-databases参数。如果你不喜欢命令行,从Sourceforge上下载automysqlbackup吧。

调整CONFIG的配置

PERL脚本MySQL Tuner是另一个强大的优化数据库性能的工具,它能够帮助你对MySQL配置来进行多处调整和修改。你可以访问该项目的官网来进一步了解它。

推荐阅读:

mysqlslap 压力测试工具

MySQL自带的性能压力测试工具mysqlslap详解

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you represent relationships using foreign keys? How do you represent relationships using foreign keys? Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

How do you create indexes on JSON columns? How do you create indexes on JSON columns? Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

See all articles