Home Database Mysql Tutorial 浅析MySQL数据库常用管理_MySQL

浅析MySQL数据库常用管理_MySQL

Jun 01, 2016 pm 01:59 PM
database

好久没有整理东西,感觉自己越来越懒,正好近期有个MYSQL的项目(一个ORACLE的应用迁移到MYSQL),以前没有接触过MYSQL,但是也知道MYSQL数据库目前广泛的应用在各种个人、商务系统中,各种技术都比较成熟。把自己学习的一些过程总结一下。

?

??? 我们在前面已经简单介绍了一些MYSQL数据库的基本操作,这一章我们将针对MYSQL数据库管理员详细介绍下MYSQL数据库的常用管理内容。

??? MYSQL的管理工具很多,我自己从网上下载了一个。mysql-gui-tools-noinstall-5.0-r12-win32,是一个不用安装的管理软件,包含四个基本工具:

??? ?MySQLAdministrator
??? ?MySQLMigrationTool
??? ?MySQLQueryBrowser
??? ?MySQLSystemTrayMonitor

??? 这些工具的具体使用都很简单,操作比较灵活,这里就不对这些工具的功能做详细介绍了,如果有兴趣大家可以自己下载下来,多使用几次就熟悉了。

??? 1 MYSQL命令行管理工具

??? (1) 首先介绍几个MYSQL命令行工具:

??? mysqld_safe、mysql.server和mysqld_multi是服务器启动脚本。

??? 注意:在Windows中不使用服务器启动脚本,我们可以使用WINDOWS命令来
???
??? 启动MYSQL服务:
??? net start mysql

??? 停止MYSQL服务:
??? net stop mysql
???
??? 或者:

C:/Program Files/MySQL/MySQL Server 5.0/bin>mysqladmin -uroot -p shutdown
Enter password: ******

??? 注:mysqld是MySQL服务器

下面的表格列举了几种WINDOS平台支持的MYSQL服务器:

浅析MySQL数据库常用管理(图一)

?WINDOWS下执行二进制安装后,在WINDOWS服务中默认的就是使用的mysqld-nt服务器。

??? 这个命令的使用我们将在后边的MYSQL服务器中详细讲解。

??? mysql_install_db初始化数据目录和初始数据库。

??? mysql是一个命令行客户程序,用于交互式或以批处理模式执行SQL语句,

??? 这个命令我们在前边的例子中已经有很多应用了,这里就不多讲述了。我们给出一个例子:


C:/Program Files/MySQL/MySQL Server 5.0/bin>mysql -u root -p -e "SELECT * FROM??????????????????????????? TEST limit 5;select * from test where id=1000" mytest
Enter password: ******
+------+------+---------------------+---------------------+
| ID?? | MC?? | DT????????????????? | RQ????????????????? |
+------+------+---------------------+---------------------+
| 1000 | test | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
|? 999 | test | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
|? 998 | test | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
|? 997 | test | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
|? 996 | test | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+------+------+---------------------+---------------------+
+------+------+---------------------+---------------------+
| ID?? | MC?? | DT????????????????? | RQ????????????????? |
+------+------+---------------------+---------------------+
| 1000 | test | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
| 1000 | test | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
| 1000 | test | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+------+------+---------------------+---------------------+


??? mysqladmin是用于管理功能的客户程序。

??? 我们可以通过mysqladmin –help来获得相关的信息,mysqladmin可以完成数据库的创建删除,修改用户密码,检查服务器状态,刷新系统变量等很多管理功能:


C:/Program Files/MySQL/MySQL Server 5.0/bin>mysqladmin -uroot -p ping
Enter password: ******
mysqld is alive

C:/Program Files/MySQL/MySQL Server 5.0/bin>mysqladmin -uroot -p status
Enter password: ******
Uptime: 17842? Threads: 2? Questions: 12? Slow queries: 0? Opens: 13? Flush tabl
es: 1? Open tables: 1? Queries per second avg: 0.001

C:/Program Files/MySQL/MySQL Server 5.0/bin>mysqladmin -uroot -p version
Enter password: ******
mysqladmin? Ver 8.41 Distrib 5.0.27, for Win32 on ia32
Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version????????? 5.0.27-community-nt
Protocol version??????? 10
Connection????????????? localhost via TCP/IP
TCP port??????????????? 3306
Uptime:???????????????? 4 hours 58 min 21 sec

Threads: 2? Questions: 13? Slow queries: 0? Opens: 13? Flush tables: 1? Open tab
les: 1? Queries per second avg: 0.001


??? 该命令我们将在MYSQL数据库用户管理、备份恢复中涉及到它的一些其他基本功能。

??? mysqlcheck执行表维护操作:


C:/Program Files/MySQL/MySQL Server 5.0/bin>mysqlcheck -uroot -p mytest
Enter password: ******
mytest.aaa???????????????????????????????????????? OK
mytest.customer??????????????????????????????????? OK
mytest.mytable???????????????????????????????????? OK
mytest.sys_tests?????????????????????????????????? OK
mytest.test??????????????????????????????????????? OK
mytest.test1?????????????????????????????????????? OK
mytest.test_isam?????????????????????????????????? OK


??? mysqldump数据库备份
??? mysqlhotcopy数据库备份
??? mysqlimport导入数据文件

??? 这几个命令工具我们将在后边的备份恢复中详细讲解。

??? mysqlshow显示信息数据库和表的相关信息。

?

C:/Program Files/MySQL/MySQL Server 5.0/bin>mysqlshow -uroot -p mytest
Enter password: ******
Database: mytest
+-----------+
|? Tables?? |
+-----------+
| aa??????? |
| aaa?????? |
| customer? |
| mytable?? |
| sys_tests |
| test????? |
| test1???? |
| test_isam |
+-----------+


??? 以下是几个可以独立于MYSQL服务器(客户端可以执行)进行操作的工作。

??? myisamchk执行表维护操作。
??? myisampack产生压缩、只读的表。
??? mysqlbinlog是处理二进制日志文件的实用工具。

??? 在MySQL中binlog的作用和Oracle中的归档日志类似, Oracle中提供了DBMS_LOGMNR来对日志文件进行分析, 来解出Redo SQL和Undo SQL, MySQL中也提供了一个名为mysqlbinlog的工具, 用来解释或取出存放在binlog中的SQL语句, 有没有Undo SQL我到是没有研究过. 最基本的使用语法如下:


mysqlbinlog [options] log_file ...

??? 在选项中, 可以指定一些过滤条件, 来解出你所想用的东西, 这样的选项有:


--database=db_name, -d db_name
--offset=N, -o N
--[start|stop]-datetime=datetime
--[start|stop]-position=N


??? 解出来的就是一条一条SQL语句了, 将这些语句执行一下, 就等于增量恢复了, 估计不是用绑定变量的, 可能在MySQL中是不是绑定变量不是很重要了。 然重要的一点是不要运行多个进程去跑, 因为这样的话, 顺序就得不到保证了。如下所示:


$ mysqlbinlog binlog.000001 >? /tmp/statements.sql
$ mysqlbinlog binlog.000002 >> /tmp/statements.sql
$ mysql -e "source /tmp/statements.sql"


??? Oracle的LogMiner不太爽是因为它不是离线的, 做成MySQL这样的倒是比较方便多了. 看到这儿也应当可以想象到, MySQL中的复制大约是什么回事了吧?

??? Mysqldumpslow

??? MySQL自带slow log的分析工具mysqldumpslow,但是没有说明。本文通过分析该脚本,介绍了其用法。slow log是MySQL根据SQL语句的执行时间设定,写入的一个文件,用于分析执行较慢的语句。

??? 只要在 my.ini 文件中配置好:


log-slow-queries = [slow_query_log_filename]

??? 即可记录超过默认的 10s 执行时间的 SQL 语句。
??? 如果要修改默认设置,可以添加:


long_query_time = 5

??? 设定为 5s 。

??? 如果要记录所有 SQL 语句,可以写入:


log-long-format

# t=time, l=lock time, r=rows


??? # at, al, 以及 ar 是对应的平均值

??? mysqldumpslow 可以接受的参数有:


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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 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 does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

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

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

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())

Tips and practices for handling Chinese garbled characters in databases with PHP Tips and practices for handling Chinese garbled characters in databases with PHP Mar 27, 2024 pm 05:21 PM

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

See all articles