Home Database Mysql Tutorial MySQL数据库规范化设计

MySQL数据库规范化设计

Jun 07, 2016 pm 04:18 PM
mysql database Standardize design

建立一个数据库表维护规范 在一个定期基础而非等到问题出现才实施数据库表的检查是一个好主意。应该考虑到建立一个预防性维护的时间表,以协助自动问题,是你可以采取措施进行修正: 执行常规的数据库备份并允许更新日志。 安排定期的常规表检查。通过检查表

  建立一个数据库表维护规范

  在一个定期基础而非等到问题出现才实施数据库表的检查是一个好主意。应该考虑到建立一个预防性维护的时间表,以协助自动问题,是你可以采取措施进行修正:

  执行常规的数据库备份并允许更新日志。

  安排定期的常规表检查。通过检查表,将减少使用备份的机会。这个工作,在Windows下使用计划任务,Unix使用cron作业(一般从运行服务器所示用的该帐号的crontab文件中调用),并且很容易实现。

  例如,你作为mysql用户运行服务器,则可以从mysql的crontab文件中建立定期检查。如果你不知道如何使用cron,应使用下列命令查看相关的Unix手册页:

  $man cron

  $man crontab

  作为服务器启动前的系统引导期间检查数据库表。及其可能会因早期的崩溃而重新启动。如果这样的花,数据库表可能已被毁坏,应该对它进行彻底检查。

  创建一个适用于定期维护的脚本

  为了运行自动的表检查,可以编写一个脚本,将目录改变为服务器数据目录并对所有的数据库表进行myisamchk和isamchk。如果你只有MyISAM表或者只有ISAM表,则只需要其中一个程序,可以将无关的那个程序从脚本中注释掉。

  该教本可以被cron调用,或者在系统启动期间被调用。

  为维护目的,,你能使用myisamchk -s检查桌子。-s,--silent选项使myisamchk和isamchk以沉默模式运行,只有当错误出现时,才仅仅打印消息。另外myisamchk支持--fast选项,该选项允许程序跳过自上次检查以来没有被修改过的人和表。

  1. 一个简单的脚本

  例如,一个较为容易理解的简单脚本,它在服务器目录中检查所有表(DATADIR应该修改成对应你系统的合适的值):

  #!/bin/sh

  cd DATADIR

  myisamchk --silent --fast */*.MYIi

  samchk --silent */*.ISM

  2. 一个较为复杂的脚本

  实用此脚本的一个潜在的问题时:如果有许多表,通配符模式‘*/*.MYI’和‘*/*.ISM’可能会由于“too many arguments(参数过多)”或者命令行超过shell允许的长度而无法使用。脚本可以进一步修改为(同样,DATADIR修改为适合你系统上的值):

  #!/bin/sh

  datadir=DATADIR

  find $dtatdir –name “*. MYI” -print | xargs myisamchk --silent --fast

  find $dtatdir –name “*. ISM” -print | xargs isamchk --silent

  当然你也可以在脚本中指定多个数据库目录。

  3. 如何执行脚本

  假定你将脚本存为check_tables,应该确保它是可执行的,当然建议你首先切换到专用户mysql:

  $su mysql

  $vi check_tables(编辑脚本,你也可以使用你喜欢的编辑器)

  $chmod +x check_tables

  手工执行,检测你的脚本是否有错误:

  $check_tables

  在理想情况下应该没有输出结果。如果系统不支持外部锁定,游客蒽那个服务器将在你检查表时改变它。此时,脚本可能会把实际没有问题的表报告呈有问题的。如果系统能够支持外部锁定,则该问题就不会出现。

  在unix中用cron定期检查表

  以下将说明如何建立脚本,使它通过cron并在系统启动期间执行。在这小节的例子中,笔者假定把脚本安装在/usr/local/mysql/bin中,你需要修改该过程来检查每个服务器数据目录中的表。你可以使用不同的check_tables拷贝来进行,或者通过修改它来接受一个命令行参数进行,该参数指定了想要检查的数据目录。

  假定对mysql用户从crontab文件中调用脚本check_tables。

  1. 首先用该用户的身份注册

  $su mysql

  2. 生成一个临时文件,捕获已经调度的任务

  $crontab –l>/tmp/entries

  3. 在生成的文件最后一行添加内容

  把这一行0 0 * * 0 /usr/local/mysql/bin/check_tables加到临时文件的最后一行:

  $echo “0 0 * * 0 /usr/local/mysql/bin/check_tables” >>/tmp/entries

  它告诉cron在每个星期日的凌晨0时运行此选项。可以按要求改变时间或安排。有关这些选项的格式,参阅crontab的手册页。

  4. 重新安排调度

  $crontab /tmp/entries

  如果检查后有任何信息,cron作业通常生成一个邮件消息给用户。由于使用--silent选项,只有表存在错误时,才会有输出,也才会有邮件信息,因此不会产生大量无用邮件信息。(你现在应该明白,脚本采用--silent选项的原因对于这样定期的维护,你的服务器最好支持外部锁定,这样在检查表时,就不会发生访问冲突的情况。如果无法做到这一点,你最好在没有用户使用服务器的时候维护,例如凌晨。

  在系统启动期间检查表

  如果你使用的是BSD风格的系统,例如OpenBSD,FreeBSD等,并且已经将服务器的启动命令增加到/etc/rc.local,要在启动期间检查表,可以在启动服务器前从相应的文件中调用check_tables。

  如果对于使用Sytem V风格的启动方法的系统,例如,大多数的商业Unix系统,其启动方法是从/etc/rc.d目录之一调用mysql.server脚本,则在数据库启动前检查表的过程比较复杂,因为这些目录中的脚本必须理解start和stop参数。

  例如,象这样编写脚本,取名mysql.check当参数时start时调用check_tables,当参数是stop时什么也不做:

  #!/bin/sh

  #See hou we sere called

  case “$1” in

  start)

  echo –n “Checking MySQL tables:”

  if [ -x /usr/local/mysql/bin/check_tables ] ; then

  /usr/local/mysql/bin/ check_tables

  fi;;

  stop)

  #don’t do anything

  ;;

  *)

  echo “Usage:$o{start|stop}”

  exit 1

  esac

  exit 0

  现在你可以安装mysql.check了,该过程类似乎在第二章介绍的让服务器自动启动的安装mysql.server的过程。必须给mysql.check一个运行级目录中较低的前缀号,才能使它在mysql.server前运行。例如,如果在运行级目录中以S99mysql.server连接到mysql.server,则应该以S98mysql.check链接到mysql.check。

  由于Linux集中了BSD和Sytem V系统的优点,所以,上面两种方法完全适用于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 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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 to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Honor Magic V3 debuts AI defocus eye protection technology: effectively alleviates the development of myopia Honor Magic V3 debuts AI defocus eye protection technology: effectively alleviates the development of myopia Jul 18, 2024 am 09:27 AM

According to news on July 12, the Honor Magic V3 series was officially released today, equipped with the new Honor Vision Soothing Oasis eye protection screen. While the screen itself has high specifications and high quality, it also pioneered the introduction of AI active eye protection technology. It is reported that the traditional way to alleviate myopia is "myopia glasses". The power of myopia glasses is evenly distributed to ensure that the central area of ​​​​sight is imaged on the retina, but the peripheral area is imaged behind the retina. The retina senses that the image is behind, promoting the eye axis direction. grow later, thereby deepening the degree. At present, one of the main ways to alleviate the development of myopia is the "defocus lens". The central area has a normal power, and the peripheral area is adjusted through optical design partitions, so that the image in the peripheral area falls in front of the retina.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

New stacking process! Xiaomi MIX Fold 4 is equipped with Jinshajiang 'three-dimensional special-shaped' battery for the first time New stacking process! Xiaomi MIX Fold 4 is equipped with Jinshajiang 'three-dimensional special-shaped' battery for the first time Jul 20, 2024 am 03:20 AM

According to news on July 19, Xiaomi MIX Fold 4, the first flagship folding new phone, was officially released tonight and is equipped with a "three-dimensional special-shaped battery" for the first time. According to reports, Xiaomi MIX Fold4 has achieved a major breakthrough in battery technology and designed an innovative "three-dimensional special-shaped battery" specifically for folding screens. Traditional folding screen devices mostly use conventional square batteries, which have low space utilization efficiency. In order to solve this problem, Xiaomi did not use the common winding battery cells, but developed a new lamination process to create a new form of battery, which greatly improved the space utilization. Battery Technology Innovation In order to accurately alternately stack positive and negative electrode sheets and ensure the safe embedding of lithium ions, Xiaomi has developed a new ultrasonic welding machine and lamination machine to improve welding and cutting accuracy.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

See all articles