- 浏览: 138040 次
文章分类
最新评论
一.启动
1.1 MySQL 进程
可以用ps 命令查看进程:
[root@rac2 ~]# ps -ef|grep mysql
root 2161 1 0 09:38 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe--datadir=/var/lib/mysql --pid-file=/var/lib/mysql/rac2.pid
mysql 2418 2161 0 09:38 ? 00:00:00 /usr/sbin/mysqld--basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin--user=mysql --log-error=/var/lib/mysql/rac2.err--pid-file=/var/lib/mysql/rac2.pid --socket=/var/lib/mysql/mysql.sock--port=3306
root 3628 3596 0 09:57 pts/1 00:00:00 grep mysql
1.1.1 mysqld — The MySQL Server
mysqld, also known as MySQL Server, is the main program that does most of the work in aMySQL installation. MySQL Server manages access to the MySQL data directorythat contains databases and tables. The data directory is also the defaultlocation for other information such as log files and status files.
When MySQLserver starts, it listens for network connections from client programs andmanages access to databases on behalf of those clients.
The mysqld programhas many options that can be specified at startup. For a complete list ofoptions, run this command:
shell> mysqld--verbose --help
MySQL Serveralso has a set of system variables that affect its operation as it runs. Systemvariables can be set at server startup, and many of them can be changed atruntime to effect dynamic server reconfiguration. MySQL Server also has a setof status variables that provide information about its operation. You canmonitor these status variables to access runtime performance characteristics.
-- mysqld 进程是MySQL Server 最核心的进程。mysqld 进程crash 或者出现异常,MySQLServer 基本上也就无法正常提供服务了。
1.1.2 mysqld_safe — MySQL Server Startup Script
mysqld_safe is the recommended way to start a mysqld server on Unix. mysqld_safe adds somesafety features such as restarting the server when an error occurs and loggingruntime information to an error log file. A description of error logging isgiven later in this section.
mysqld_safe triesto start an executable named mysqld. To override the default behavior andspecify explicitly the name of the server you want to run, specify a --mysqld or--mysqld-version option to mysqld_safe. You can also use --ledir to indicate thedirectory where mysqld_safe should look for the server.
mysqld_safe readsall options from the [mysqld], [server], and [mysqld_safe] sections in optionfiles. For example, if you specify a [mysqld] section like this, mysqld_safe willfind and use the --log-error option:
[mysqld]
log-error=error.log
For backwardcompatibility, mysqld_safe also reads [safe_mysqld] sections, although youshould rename such sections to [mysqld_safe] in MySQL 5.5 installations.
--mysqld_safe 会帮助我们来监控mysqld 进程的状态,当mysqld 进程crash 之后,mysqld_safe会马上帮助我们重启mysqld 进程。但前提是我们必须通过mysqld_safe 来启动MySQLServer,这也是MySQL AB 强烈推荐的做法。
1.1.3 mysql.server — MySQL Server Startup Script
MySQLdistributions on Unix include a script named mysql.server. It can be used onsystems such as Linux and Solaris that use System V-style run directories tostart and stop system services. It is also used by the Mac OS X Startup Itemfor MySQL.
mysql.server canbe found in the support-files directory under your MySQL installation directoryor in a MySQL source distribution.
--mysql.server 脚本放在support-files 目录下。 即:/usr/share/mysql
If you use theLinux server RPM package (MySQL-server-VERSION.rpm), the mysql.server scriptwill be installed in the /etc/init.d directory with the name mysql. You neednot install it manually. See Section 2.5.1, “Installing MySQL from RPM Packages on Linux”, for more information onthe Linux RPM packages.
--如果我们使用RPM 包来安装的MySQL,那么mysql.server 脚本会自动安装到/etc/init.d目录下,并且重名为mysql。 我们不需要手工的来安装
[root@rac2 mysql]# ls /etc/init.d/mysql
/etc/init.d/mysql
Some vendorsprovide RPM packages that install a startup script under a different name suchas mysqld.
If you installMySQL from a source distribution or using a binary distribution format thatdoes not install mysql.server automatically, you can install it manually.Instructions are provided in Section 2.10.1.2, “Starting and Stopping MySQLAutomatically”.
mysql.server readsoptions from the [mysql.server] and [mysqld] sections of option files. Forbackward compatibility, it also reads [mysql_server] sections, although youshould rename such sections to [mysql.server] when using MySQL 5.5.
--mysql.server 从参数文件里读取[mysql.server]和[mysqld]段的的信息。
1.1.4 mysqld_multi — Manage Multiple MySQL Servers
mysqld_multi is designedto manage several mysqld processes that listen for connections on differentUnix socket files and TCP/IP ports. It can start or stop servers, or reporttheir current status.
--如果我们的一个节点上有多个mysql,可以使用mysqld_multi 来管理。
mysqld_multi searchesfor groups named [mysqldN] in my.cnf (or in the file named by the --config-fileoption). N can be any positive integer. This number is referred to in thefollowing discussion as the option group number, or GNR. Group numbersdistinguish option groups from one another and are used as arguments to mysqld_multito specify which servers you want to start, stop, or obtain a status reportfor. Options listed in these groups are the same that you would use in the [mysqld]group used for starting mysqld. (See, for example, Section 2.10.1.2, “Startingand Stopping MySQL Automatically”.) However, when using multiple servers, it isnecessary that each one use its own value for options such as the Unix socketfile and TCP/IP port number. For more informationon which options must beunique per server in a multiple-server environment, see Section 5.6, “RunningMultiple MySQL Instances on One Machine”.
-- mysqld_multi 会在my.cnf 里搜索mysqldN的参数配置。
以上都是相关进程的大概内容,详细内容可以参考MySQL 5.5的官方文档的4.3 小节:MySQLServer and Server-Startup Programs
1.2 启动进程的参数
The MySQL servermaintains many system variables that indicate how it is configured. Each systemvariable has a default value. System variables can be set at server startupusing options on the command line or in an option file. Most of them can bechanged dynamically while the server is running by means of the SET statement,which enables you to modify operation of the server without having to stop andrestart it. You can refer to system variable values in expressions
在启动MySQL 时,我们可以手工的在命令后面指定相关的参数, 如上面ps 的命令显示的一样:
/usr/sbin/mysqld --basedir=/usr--datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql--log-error=/var/lib/mysql/rac2.err --pid-file=/var/lib/mysql/rac2.pid--socket=/var/lib/mysql/mysql.sock --port=3306
这里是我们的默认的一个参数值,我们可以通过如下命令来查看MySQL的默认参数的具体值:
(1)To see the values that a serverwill use based on its compiled-in defaults and any option files that it reads,use this command:
mysqld --verbose--help
(2)To see the values that a serverwill use based on its compiled-in defaults, ignoring the settings in any optionfiles, use this command:
mysqld --no-defaults --verbose --help
如果每次启动都手工的指定参数比较麻烦,我们可以把相关的参数配置到参数文件里,在MySQL启动时会读取这些文件,参数文件的位置:
On Unix, Linux and Mac OS X, MySQL programsread startup options from the following files, in the specified order (topitems are used first).
注意上表中参数文件的顺序,MySQL 会按照从上往下的顺序优先使用上面的参数文件,这个就类似与Oracle的SPFILE,PFILE和 init 文件的读取顺序一样。
~ representsthe current user's home directory (the value of $HOME).
SYSCONFDIR representsthedirectory specified with the SYSCONFDIR option to CMake when MySQL wasbuilt. By default, this is the etc directory located under the compiled-ininstallation directory.
MYSQL_HOME is anenvironment variable containing the path to the directory in which theserver-specific my.cnf file resides. If MYSQL_HOME is not set and you start theserver using the mysqld_safe program, mysqld_safe attempts to set MYSQL_HOME asfollows:
(1)Let BASEDIR and DATADIR representthe path names of the MySQL base directory and data directory, respectively.
(2)If there is a my.cnf file in DATADIRbut not in BASEDIR, mysqld_safe sets MYSQL_HOME to DATADIR.
(3)Otherwise, if MYSQL_HOME is not setand there is no my.cnf file in DATADIR, mysqld_safe sets MYSQL_HOME to BASEDIR.
In MySQL 5.5,use of DATADIR as the location for my.cnf is deprecated.
--在MySQL 5.5中,my.cnf 文件已经不放在DATADIR目录下。
Typically, DATADIRis /usr/local/mysql/data for a binary installation or /usr/local/var for asource installation. Note that this is the data directory location that wasspecified at configuration time, not the one specified with the --datadir optionwhen mysqld starts. Use of --datadir at runtime has no effect on where theserver looks for option files, because it looks for them before processing anyoptions.
我的测试版本:
mysql> selectversion(),current_user();
+------------+----------------+
| version() | current_user() |
+------------+----------------+
| 5.5.15-log |root@localhost |
+------------+----------------+
1 row in set(0.02 sec)
里并没有my.cnf 文件,只在/usr/share/mysql目录下面有几个类似的文件:
[root@rac2 mysql]# pwd
/usr/share/mysql
[root@rac2 mysql]# ls my*
my-huge.cnf my-small.cnf mysql_system_tables_data.sql
my-innodb-heavy-4G.cnf mysqld_multi.server mysql_system_tables.sql
my-large.cnf mysql-log-rotate mysql_test_data_timezone.sql
my-medium.cnf mysql.server
我们copy 一个到/etc 目录下:
[root@rac2 mysql]#cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
从上面了解到,MySQL 在启动时会先使用/etc/my.cnf的配置文件,如果没有在使用其他的文件,所以我们修改该配置文件里的相关参数即可。
二.关闭
2.1 官网对shutdown的说明
The server shutdown process takes place asfollows:
1. The shutdown processis initiated.
This can occurinitiated several ways. For example, a user with the SHUTDOWN privilege canexecute a mysqladmin shutdown command. mysqladmin can be used on any platformsupported by MySQL. Other operating system-specific shutdown initiation methodsare possible as well: The server shuts down on Unix when it receives a SIGTERM signal.A server running as a service on Windows shuts down when the services managertells it to.
2. The server creates ashutdown thread if necessary.
Depending on howshutdown was initiated, the server might create a thread to handle the shutdownprocess. If shutdown was requested by a client, a shutdown thread is created.If shutdown is the result of receiving a SIGTERM signal, the signal threadmight handle shutdown itself, or it might create a separate thread to do so. Ifthe server tries to create a shutdown thread and cannot (for example, if memoryis exhausted), it issues a diagnostic message that appears in the error log:
Error: Can't create thread to kill server
3. The server stopsaccepting new connections.
To prevent newactivity from being initiated during shutdown, the server stops accepting newclient connections by closing the handlers for the network interfaces to whichit normally listens for connections: the TCP/IP port, the Unix socket file, theWindows named pipe, and shared memory on Windows.
4. The server terminatescurrent activity.
For each threadassociated with a client connection, the server breaks the connection to theclient and marks the thread as killed. Threads die when they notice that theyare so marked. Threads for idle connections die quickly. Threads that currentlyare processing statements check their state periodically and take longer todie. For additional information about thread termination, see Section 12.7.6.4,“KILL Syntax”, in particular for the instructions about killed REPAIR TABLE or OPTIMIZETABLE operations on MyISAM tables.
For threads thathave an open transaction, the transaction is rolled back. Note that if a threadis updating a nontransactional table, an operation such as a multiple-row UPDATEor INSERT may leave the table partially updated because the operation canterminate before completion. If the server is a master replication server, ittreats threads associated with currently connected slaves like other clientthreads. That is, each one is marked as killed and exits when it next checksits state.
If the server isa slave replication server, it stops the the I/O and SQL threads, if they areactive, before marking client threads as killed. The SQL thread is permitted tofinish its current statement (to avoid causing replication problems), and thenstops. If the SQL thread was in the middle of a transaction at this point, thetransaction is rolled back.
If the slave isupdating a non-transactional table when it is forcibly killed, the slave's datamay become inconsistent with the master.
5. The server shuts downor closes storage engines.
At this stage,the server flushes the table cache and closes all open tables. Each storageengine performs any actions necessary for tables that it manages. For example, MyISAMflushes any pending index writes for a table. InnoDB flushes its buffer pool todisk (unless innodb_fast_shutdown is 2), writes the current LSN to the tablespace,and terminates its own internal threads.
6. The server exits.
三.相关示例
3.1 部署MySQL自启动
To start andstop MySQL automatically on your server, you need to add start and stopcommands to the appropriate places in your /etc/rc* files.
--如果配置MySQL的自动启用和关闭,需要讲相关命令添加到/etc/rc*目录里。
If you use theLinux server RPM package (MySQL-server-VERSION.rpm), or a native Linux packageinstallation, the mysql.server script may be installed in the /etc/init.d directorywith the name mysql. See Section 2.5.1, “Installing MySQL from RPM Packages onLinux”, for more information on the Linux RPM packages.Some vendors provide RPMpackages that install a startup script under a different name such as mysqld.
--如果是使用RPM包来安装的,已经部署到/etc/init.d/mysql了脚本,不需要其他配置。
If you installMySQL from a source distribution or using a binary distribution format thatdoes not install mysql.server automatically, you can install it manually. Thescript can be found in the support-files directory under the MySQL installationdirectory or in a MySQL source tree.
To install mysql.servermanually, copy it to the /etc/init.d directory with the name mysql, and thenmake it executable. Do this by changing location into the appropriate directorywhere mysql.server is located and executing these commands:
--如果手工的部署启动启动脚本,执行如下2个命令:
shell> cp mysql.server /etc/init.d/mysql
shell> chmod +x /etc/init.d/mysql
Note:
Older Red Hatsystems use the /etc/rc.d/init.d directory rather than /etc/init.d. Adjust thepreceding commands accordingly.
--老版本的Redhat 使用/etc/rc.d/init.d 代替/etc/init.d,所以需要创建一个link。
Alternatively,first create /etc/init.d as a symbolic link that points to /etc/rc.d/init.d:
shell> cd /etc
shell> ln -s rc.d/init.d .
After installingthe script, the commands needed to activate it to run at system startup dependon your operating system.
--脚本安装完之后,需要激活自启动。
On Linux, you can use chkconfig:
shell> chkconfig --add mysql
On some Linux systems, the followingcommand also seems to be necessary to fully enable the mysql script:
shell>chkconfig --level 345 mysql on
3.2 手工启动和关闭MySQL 示例
[root@rac2 /]#/usr/share/mysql/mysql.server stop
Shutting down MySQL. [ OK ]
[root@rac2 /]#/usr/share/mysql/mysql.server start
Starting MySQL.... [ OK ]
[root@rac2 /]# service mysql stop
Shutting down MySQL. [ OK ]
[root@rac2 /]# service mysql start
Starting MySQL...... [ OK ]
mysql.server stop stops the server by sending a signal to it. You can also stop the servermanually by executing mysqladmin shutdown.
[root@rac2 run]# mysqladmin shutdown
[root@rac2 run]# ps -ef|grep mysql
root 30997 3596 0 14:51 pts/1 00:00:00 grep mysql
Mysqladmin 不能用来启动mysql。只能用来关闭。没有对应的启动语法。这个具体可以通过如下命令查看:
[root@rac2 run]# mysqladmin --help
另一个需要注意的问题,/var/lib/mysql/mysql.sock 文件仅在mysql 进程启动时才有,一旦mysql关闭,改文件也就自动清除。
[root@rac2 run]# ls /var/lib/mysql/mysql.sock
ls: /var/lib/mysql/mysql.sock: No such fileor directory
[root@rac2 run]# service mysql start
Starting MySQL..... [ OK ]
[root@rac2 run]# ls/var/lib/mysql/mysql.sock
/var/lib/mysql/mysql.sock
发表评论
-
(转)mysql 格式化日期 DATE_FORMAT,FROM_UNIXTIME,UNIX_TIME等
2013-03-01 16:40 11151. DATE_FORMAT() 函数用于以不同的格式显示日 ... -
(转载)mysql通过关联修改表中两个字段的值
2013-01-21 15:28 1117mysql通过关联修改表中 ... -
(转)linux ps命令和kill命令概述
2012-12-28 12:02 824使用linux操作系统,难免遇到一些软件“卡壳”的问题,这时就 ... -
转Error Code: 2006 - MySQL server has gone away(Error Code: 2006 - MySQL 服务器已离线)
2012-12-27 11:14 928刚才在导入数据库的时候 SQLyog 报错了Error Cod ... -
mysql添加用户和用户权限
2013-01-21 15:29 1846Mysql添加用户使用可以对mysql数据库用户表有操作权限的 ... -
主从服务器配置
2012-12-18 17:06 744安装环境:主服务器::192.168.1.10从服务器::19 ... -
同机MySQL主从同步设置
2012-12-18 17:02 7901、 配置主库my.ini port=3306 data ... -
MySQL主从复制与读写分离
2012-12-18 16:05 710MySQL主从复制(M ... -
mysql主从复制
2012-12-18 15:23 646mysql主从复制 怎么安装mysql数据库,这里不说了,只 ... -
(转载)MySQL的log-bin的日志功能
2012-12-18 15:10 642装mysql,运行一段时间后 ... -
(转载)mysql 远程连接速度慢的解决方案
2012-12-14 15:35 687PHP远程连接MYSQL速度慢,有时远程连接到M ... -
(转载)MySQL常用操作基本操作
2012-12-13 11:18 670MySQL常用操作基本操作,以下都是MySQL5.0下测试通过 ... -
MySQL 5.5版本下my.ini内[mysqld]项中不能再写default-character-set=utf8
2012-12-13 11:12 746原来在5.1版本时,为了解决中文乱码问题设置默认字符集为utf ... -
mysql从一个表向另一个表转移数据的存储过程
2012-12-11 10:59 822DELIMITER $$ USE `sms`$$ DROP ... -
(转载)MYSQL数据表损坏的原因分析和修复方法
2012-11-28 09:23 700一、表损坏的原因分析 以下原因是导致mysql 表毁坏的 ... -
linux下查看磁盘空间
2012-11-23 16:40 654如果要查看磁盘还剩多 ... -
MySQL日期时间函数大全
2012-11-17 10:38 638DAYOFWEEK(date) 返回日期date是星期几(1 ... -
(转载)Windows下安装MySQL最佳实践
2012-10-15 17:41 715一、背景 其实在Windows下安装MySQL可谓是一件很简 ... -
Linux环境下如何同时启动多个Tomcat服务器
2012-09-29 14:44 705所用Tomcat服务器都为zip版,非安装版。以两个为例: ... -
同时启动多个Tomcat 和 Linux部署多个tomcat
2012-09-29 14:36 3091同时启动多个Tomcat 出于某种需要,需要 ...
相关推荐
Linux 中 MySQL 开机自动启动 3 种方法 Linux 中 MySQL 开机自动启动是指在 Linux 系统启动时自动启动 MySQL 服务,以便提供数据库服务。本文将介绍三种实现 Linux 中 MySQL 开机自动启动的方法。 方法一:使用 ...
本文将介绍MySQL 5.7版本在Linux上的部署、启动以及遇到问题时的排查技巧。特别适合新手进行学习。部署MySQL 5.7 For Linux涉及文件系统的选择、安全配置以及性能优化等关键步骤。 首先,文件系统的兼容性对于MySQL...
### Linux下MySQL安装配置使用指南知识点梳理 #### 一、引言 Linux用户群体庞大,其中不少人对Linux...通过这份指南,用户可以快速掌握Linux下MySQL的安装与基本使用,从而在工作中快速入门Linux环境下的数据库管理。
与启动命令类似,这个命令也是通过调用`mysql.server`脚本来实现MySQL服务的安全关闭。 - **命令格式**:`/usr/local/mysql/share/mysql/mysql.server stop` - **执行用户**:同样建议使用root用户或其他具有足够...
Linux 下安装 MySQL 服务器 Linux 下安装 MySQL 服务器是指在 Linux 操作系统中安装和配置 MySQL 数据库服务器的过程。MySQL 是一种关系型数据库管理系统,广泛应用于 Web 应用程序中。 知识点: 1. 安装 MySQL ...
### Linux 下启动、停止与重启 MySQL 服务的知识点详解 #### 一、启动 MySQL 服务的方法 在 Linux 系统中启动 MySQL 数据库服务是非常常见的操作。根据给定的文件内容,主要有以下三种方法来启动 MySQL 服务: 1....
Linux下自动备份Mysql文件shell脚本。
本篇文章将详细讲解“mysql-connector-java”在Linux下作为MySQL的JDBC驱动Jar包的相关知识点。 1. JDBC简介: JDBC是Java平台上的一个标准接口,它允许Java应用程序通过API与各种类型的数据库进行通信。JDBC提供...
标题中的“Linux下安装Mysql”意味着我们将关注如何在Linux操作系统上安装MySQL数据库服务。这通常包括下载RPM包,验证依赖关系,然后进行安装。 描述中的“Linux mysql rpm安装包”指的是我们使用RPM包来安装MySQL...
### Linux下MySQL的配置详解 #### 一、安装MySQL 在Linux系统中安装MySQL可以通过RPM包进行。这里提供了一个示例命令: ```bash rpm -ivh MySQL-server-4.1.22-0.glibc23.i386.rpm --nodeps rpm -ivh MySQL-...
本篇将详细介绍在Linux下通过编译安装MySQL 5.6.29的过程,这给用户提供了更多的自定义选项,以便优化配置和满足特定需求。 首先,我们需要下载MySQL的源码包。根据提供的信息,这里已经有了`mysql-5.6.29`的压缩...
linux下mysql驱动程序jar包
Linux 下 MYSQL 安装与卸载 Linux 系统下,MYSQL 是一种非常流行的关系数据库管理系统。它能够提供高效、可靠的数据存储和管理服务。然而,在 Linux 系统下安装和卸载 MYSQL 却是一件复杂的事情。下面我们将详细...
【Linux下MySQL学习笔记】 MySQL是一款广泛应用于互联网的开源关系型数据库管理系统,尤其在Linux操作系统上,其稳定性和性能表现尤为突出。本笔记主要针对在Linux环境下安装、配置、管理和优化MySQL的过程进行详解...
通过以上步骤,我们不仅完成了MySQL 5.6在Linux下的安装与配置,还了解了一些基本的SQL命令操作。这为后续的开发工作打下了坚实的基础。对于初学者来说,这是一个很好的起点,而对于进阶学习者而言,则可以在此基础...
【Linux下MySQL安装配置】 在Linux操作系统中安装和配置MySQL数据库是系统管理的重要部分,尤其对于需要处理数据存储和检索的应用程序来说。MySQL是一个流行的开源关系型数据库管理系统,广泛应用于Web应用程序、...
Linux-MySQL 实验指南 本文档提供了一个详细的 Linux-MySQL 实验指南,旨在帮助用户快速掌握 Linux-MySQL 的安装、配置和使用。实验分为 13 个步骤,涵盖了从挂载光盘镜像到进入 MySQL 数据库的所有过程。 挂载...
标题"Linux安装MySQL-devel"指向了一个具体的操作任务,即在Linux环境下安装MySQL的开发库。在这个过程中,我们将主要关注以下知识点: 1. **MySQL-devel组件**:MySQL-devel包含了用于编译MySQL客户端和服务器端...
Linux 下彻底卸载 MySQL Linux 操作系统下彻底卸载 MySQL 是一个相对复杂的过程,需要逐步完成多个步骤来确保 MySQL 被完全卸载。本文将指导您如何彻底卸载 Linux 下的 MySQL。 步骤 1:查找以前是否装有 MySQL ...
在Linux环境下进行MySQL开发是一项重要的技能,特别是在大数据处理、企业级应用和服务中,MySQL作为流行的开源关系型数据库管理系统,其高效稳定性和丰富的功能使得它在IT行业中占据着举足轻重的地位。本篇将深入...