MySQL database startup and shutdown, mysql database Shutdown

Source: Internet
Author: User

MySQL database startup and shutdown, mysql database Shutdown

MySQL database server usually refers to mysqld, while command line mysql is a mysql client program. These two concepts are often confusing. Generally, the mysql server is started to start the mysqld process. After mysqld is started, you can connect to the mysql server through mysql. This article describes how to start a mysql server and how to disable it.

 

1. Start directly with mysqld
Mysqld is the mysql server. You can call this command to directly start the mysql server.
Mysqld reads [mysqld] and [server] consumer group content from the configuration file, or you can directly follow the parameter through the command line.
The order in which the mysqld server reads configuration files. If multiple configuration files exist and the same parameter has multiple values, the last read prevails.
Command line parameters have the highest priority

# Current mysql server running environment
[Root @ rhel64a ~] # Cat/etc/issue
Red Hat Enterprise Linux Server release 6.4 (Santiago)

# View the default mysqld startup options
[Root @ rhel64a ~] # Mysqld -- print-defaults
Mysqld wowould have been started with the following arguments:
-- Socket =/tmp/mysql3306.sock -- port = 3306 -- pid-file =/var/lib/mysql/my3306.pid -- user = mysql -- server-id = 3306 -- federated
[Root @ rhel64a ~] # Ps-ef | grep mysql
Root 2963 2840 0 00:00:00 pts/0 grep mysql

# Author: Leshami
# Blog: http://blog.csdn.net/leshami

# View the priority of the mysqld STARTUP configuration file
[Root @ rhel64a ~] # Mysqld -- verbose -- help | grep-A 1 "Default options"
Default options are read from the following files in the given order:
/Etc/my. cnf/etc/mysql/my. cnf/usr/etc/my. cnf ~ /. My. cnf

# View the current my. cnf configuration file
[Root @ rhel64a ~] # Grep-v ^ #/etc/my. cnf | head-n 7
[Mysqld]
Socket =/tmp/mysql3306.sock
Port = 3306
Pid-file =/var/lib/mysql/my3306.pid
User = mysql
Servers-id = 3306
Federated

# Clearing the error log file of the current mysql Server
[Root @ rhel64a ~] # Cat/dev/null>/var/lib/mysql/rhel64a.ycdata.net. err
[Root @ rhel64a ~] # Mysqld & # Start the mysqld Server
[1] 3480
[Root @ rhel64a ~] #2014-10-25 14:20:42 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated.
Please use -- explicit_defaults_for_timestamp server option (see documentation for more details ).
14:20:42 3480 [Note] InnoDB: Using atomics to ref count buffer pool pages
14:20:42 3480 [Note] InnoDB: The InnoDB memory heap is disabled
14:20:42 3480 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
14:20:42 3480 [Note] InnoDB: Memory barrier is not used
14:20:42 3480 [Note] InnoDB: Compressed tables use zlib 1.2.3
14:20:42 3480 [Note] InnoDB: Using Linux native AIO
14:20:42 3480 [Note] InnoDB: Using CPU crc32 instructions
14:20:42 3480 [Note] InnoDB: Initializing buffer pool, size = 128.0 M
14:20:42 3480 [Note] InnoDB: Completed initialization of buffer pool
14:20:42 3480 [Note] InnoDB: Highest supported file format is Barracuda.
14:20:42 3480 [Note] InnoDB: 128 rollback segment (s) are active.
14:20:42 3480 [Note] InnoDB: Waiting for purge to start
14:20:42 3480 [Note] InnoDB: 5.6.21 started; log sequence number 1626087
14:20:42 3480 [Note] Server hostname (bind-address): '*'; port: 3306
14:20:42 3480 [Note] IPv6 is available.
14:20:42 3480 [Note]-': 'resolves '::';
14:20:42 3480 [Note] Server socket created on IP :'::'.
14:20:42 3480 [Note] Event schedents: Loaded 0 events
14:20:42 3480 [Note] mysqld: ready for connections.
Version: '5. 100' socket: '/tmp/mysql3306.sock' port: 6.21 MySQL Community Server (GPL) l

# The mysqld process can be queried below
[Root @ rhel64a ~] # Ps-ef | grep mysql | grep-v grep
Mysql 18240 2641 1 00:00:00 pts/2 mysqld
[Root @ rhel64a ~] # Mysql-uroot-pxxx-P3306 -- protocol = tcp
Root @ localhost [(none)]> show variables like 'version ';
+ --------------- + -------- +
| Variable_name | Value |
+ --------------- + -------- +
| Version | 5.6.21 |
+ --------------- + -------- +

 

2. mysql secure startup (mysqld_safe)
Mysqld_safe is a shell script that calls mysqld to start the mysql server and listen to the server.
If the mysqld process ends abnormally, mysqld_safe will automatically restart mysqld
Mysql_safe reads [mysqld], [server], [mysqld_safe] and other options from the configuration file. To ensure backward compatibility, mysql_safe also reads the [safe_mysql] Option.

# Use mysqladmin to disable the mysql server that was previously started
[Root @ rhel64a ~] # Mysqladmin-uroot-pxxx shutdown-S/tmp/mysql3306.sock
[Root @ rhel64a ~] # Ps-ef | grep mysql
Root 4262 3418 0 00:00:00 pts/4 grep mysql

# Use mysqld_safe to start the instance
[Root @ rhel64a ~] # Mysqld_safe -- user = mysql &
[1] 4273
[Root @ rhel64a ~] #141025 14:40:34 mysqld_safe Logging to '/var/lib/mysql/rhel64a.ycdata.net. err '.
141025 14:40:34 mysqld_safe Starting mysqld daemon with databases from/var/lib/mysql

# View background processes after startup
[Root @ rhel64a ~] # Ps-ef | grep mysql | grep-v grep # The mysqld process is 4438, and its parent process is 4273, that is, mysqld_safe
Root 4273 3418 0 00:00:00 pts/4/bin/sh/usr/bin/mysqld_safe -- user = mysql
Mysql 4438 4273 0 00:00:00 pts/4/usr/sbin/mysqld -- basedir =/usr -- datadir =/var/lib/mysql
-- Plugin-dir =/usr/lib64/mysql/plugin -- user = mysql -- log-error =/var/lib/mysql/rhel64a.ycdata.net. err
-- Pid-file =/var/lib/mysql/my3306.pid -- socket =/tmp/mysql3306.sock -- port = 3306

[Root @ rhel64a ~] # Mysql-uroot-pxxx-P3306 -- protocol = tcp
Root @ localhost [(none)]> system kill-9 4438 # force kill mysqld Process
Root @ localhost [(none)]>/usr/bin/mysqld_safe: line 166: 4438 Killed # The following nohup is the mysql startup process.
Nohup/usr/sbin/mysqld -- basedir =/usr -- datadir =/var/lib/mysql -- plugin-dir =/usr/lib64/mysql/plugin
-- User = mysql -- log-error =/var/lib/mysql/rhel64a.ycdata.net. err -- pid-file =/var/lib/mysql/my3306.pid
-- Socket =/tmp/mysql3306.sock -- port = 3306 </dev/null>/var/lib/mysql/rhel64a.ycdata.net. err 2> & 1
141025 14:42:29 mysqld_safe Number of processes running now: 0
141025 14:42:29 mysqld_safe mysqld restarted # prompt that the process has been restarted automatically

# Verify that mysqld has been restarted successfully
Root @ localhost [(none)]> system ps-ef | grep mysql | grep-v grep
Root 4273 3418 0 00:00:00 pts/4/bin/sh/usr/bin/mysqld_safe -- user = mysql
Root 4517 3418 0 00:00:00 pts/4 mysql-uroot-px x-P3306 -- protocol = tcp
Mysql 4553 4273 0 00:00:00 pts/4/usr/sbin/mysqld -- basedir =/usr -- datadir =/var/lib/mysql
-- Plugin-dir =/usr/lib64/mysql/plugin -- user = mysql -- log-error =/var/lib/mysql/rhel64a.ycdata.net. err
-- Pid-file =/var/lib/mysql/my3306.pid -- socket =/tmp/mysql3306.sock -- port = 3306

Root @ localhost [(none)]> select databases ();
ERROR 2013 (HY000): Lost connection to MySQL server during query # the connection has been disconnected because the mysqld process is killed.
Root @ localhost [(none)]> connect # reconnect
Connection id: 1
Current database: *** NONE ***

Root @ localhost [(none)]> select database ();
+ ------------ +
| Database () |
+ ------------ +
| NULL |
+ ------------ +

 

3. mysql (mysql. server) Service Startup
Mysql. server is a shell script that calls mysqld_safe. The script reads the [mysql. server] and [mysqld] options of the configuration file.
For backward compatibility, the [mysql_server] option will also be read

# First, use mysqladmin to disable the existing mysqld.
Root @ localhost [(none)]> system mysqladmin-uroot-pxxx shutdown-S/tmp/mysql3306.sock
Warning: Using a password on the command line interface can be insecure.
141025 14:47:18 mysqld_safe mysqld from pid file/var/lib/mysql/my3306.pid ended
Root @ localhost [(none)]> exit
Bye
[1] + Done mysqld_safe -- user = mysql

[Root @ rhel64a ~] # Ls-hltr/etc/init. d/mysql #/etc/init. d directory has configured the mysql STARTUP script. Add
-Rwxr-xr-x 1 root 11 K Sep 11/etc/init. d/mysql # This startup script is automatically added if rpm is installed.

[Root @ rhel64a ~] #/Etc/init. d/mysql -- help # obtain mysql STARTUP script help
Usage: mysql {start | stop | restart | reload | force-reload | status} [MySQL server options]
[Root @ rhel64a ~] #/Etc/init. d/mysql start # start mysqld
Starting MySQL. [OK]
[Root @ rhel64a ~] #/Etc/init. d/mysql stop # stop mysqld
Shutting down MySQL... [OK]

# By default, mysql has been added to the system directory as a service and configured to start automatically, as shown below:
[Root @ rhel64a ~] # Chkconfig -- list | grep mysql
Mysql 0: off 1: off 2: on 3: on 4: on 5: on 6: off

Root @ localhost [(none)]> system ps-ef | grep mysql | grep-v grep
Root 5127 1 0 00:00:00 pts/4/bin/sh/usr/bin/mysqld_safe
-- Datadir =/var/lib/mysql -- pid-file =/var/lib/mysql/my3306.pid
Mysql 5306 5127 0 00:00:07 pts/4/usr/sbin/mysqld -- basedir =/usr -- datadir =/var/lib/mysql
-- Plugin-dir =/usr/lib64/mysql/plugin -- user = mysql -- log-error =/var/lib/mysql/rhel64a.ycdata.net. err
-- Pid-file =/var/lib/mysql/my3306.pid -- socket =/tmp/mysql3306.sock -- port = 3306
Root 5340 3418 0 00:00:00 pts/4 mysql-uroot-px x-P3306 -- protocol = tcp

# Start the mysql server through the service method, mysql. server calls mysqld_safe, and mysql_safe calls mysqld
# From the above method, we can see that no matter what startup method, mysqld will eventually be called. Because mysqld is a mysql server.

 

4. Shut down the mysql server
In the previous demonstration, we used the mysqladmin and service methods to close the instance. You can close the database in these two ways. Do not demo any more.
# Obtain the help information of the mysqladmin command line, including shutdown
[Root @ rhel64a ~] # Mysqladmin -- help | grep shutdown | grep server
Shutdown Take server down

 

5. Summary
A. Understand mysqld and mysql. Mysqld is a mysql server or an instance on a server that provides client access. Mysql is the client access management tool.
B. You can start the mysql server (mysqld, mysqld_safe, service mysql start) in three ways)
C. For mysqld_safe and service mysql start methods, the mysqld_safe method is always called to start the mysqld process, so as to prevent the mysqld from unexpected downtime and provide the Restart service.
D. You can use mysqladmin shutdown and service mysql stop to terminate the mysql server.
E. For the startup and shutdown of multiple instances, see install MySQL 5.6 (RPM mode) in Linux MySQL source code)
F. For mysql installation, see MySQL multi-instance configuration (1) MySQL multi-instance configuration (2)


How can I temporarily close the mysql database?

You can close the mysql service and start it again when it is used. Run services. msc and find out what mysql is about to pause.

Mysql database startup Problems

The problem lies not in the manual and automatic settings, but in your operations. You have modified the mysql configuration, resulting in service failure. The best way is to reinstall it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.