MySQL server startup and Shutdown

Source: Internet
Author: User
Tags central time zone root access


This chapter only applies to Unix systems. If you are running Windows, skip this chapter because the section "obtain and install software" in Appendix A contains all the required startup and shutdown commands.
Call the commands provided in this Chapter
For simplicity, in most cases, such as mysqla D m I n and mysqldump Program No-h,-u, or-P options are provided in this Chapter. I assume that you will call these programs with any options required to connect to the server.

Run the MySQL server with a non-privileged user account

Before discussing how to start the server, consider which account should be run when the server is started. The server can be started manually or automatically. If it is manually started, the server runs as a Unix user (you just registered as this user ). That is, if the author registers with Paul and starts the server, it will run as Paul. If you use the s u command to switch the user to the root and then start the server, the server runs as root.
However, the server may not be started manually most of the time. You are likely to schedule the server to run automatically as part of the standard boot process during system boot. In UNIX, the startup process is executed by the system as a Unix Ro o t user, and any process started in the process runs with the root permission.
Note the two goals of the MySQL server startup process:
The server must be started as a non-root user. In general, unless the process really requires root access, and MySQL cannot do so, the capabilities of any process should be limited.
The server must always run as the same user. The server sometimes runs as a user and sometimes as another user. This will cause the file and directory to be created under the data with different ownership, and even cause the server to be unable to access the database or table. This problem can be avoided by running the server as the same user.
To run the database as a standard, unauthenticated user, follow these steps:
1) Select the account used to run the server. Mysqld can run as any user, but obviously it only creates a separate account for the MySQL activity. You can also specify a group for MySQL. I name these users and groups called mysqladm and mysqlg R p. If you use other names, replace them with mysqladm and mysqlgrp in this book.
If you have installed MySQL under your account and the system does not have specific management permissions, you can run the server under your ID. In this case, replace mysqladm and mysqlgrp with your own registration name and group name.
If you use the RPM file to install MySql in RedHat Linux, the installer automatically creates an account under MySQL. Replace mysqladm with this name.
2) If necessary, you can use a Common Account creation process (a C count-C r e a t I o n) to create a server account. This operation must be performed as root.
3) shut down the server (if it is running ).
4) modify the ownership of Data Directories and any subdirectories and files so that mysqladm users can have them. For example, if the data directory is/us r/L o c a l/v a r, you can set the ownership of the mysqladm user as follows:
# Move CD/usr/local/var TO THE DATA DIRECTORY
# Chown-r mysqladmin. mysqlgrp set the ownership of all directories and files
5) modify the permission of Data Directories and any subdirectories and files so that only mysqladm users can access them. Setting this method is a good security precaution to prevent access by others. If the data directory is/us r/L o c a l/v a r, you can use mysqladm to set everything you should do as follows (you need to run these commands as root):
# Move CD/usr/local/var TO THE DATA DIRECTORY
# Chmod-r go-rwx make everything accessible only to mysqladm
Observe the symbolic connection when setting the ownership and method of the data directory and its content. You need to track the symbolic connection and modify the ownership and method of the file or directory to which it points. If the directory to which these connection files are located does not belong to you, this may cause trouble. Therefore, you must be a root user.
After completing the preceding process, make sure that the server is always started, whether registered as mysqladm or as a root user. In the latter, make sure that the -- user = mysqladm option is specified so that the server can switch its user ID to mysqla d M (this option can also be used during system startup ).
The -- user option is added to MySQL of mysql3.22. If your version is earlier than mysql3.22, you can run the su command to instruct the system to run the server under the specified account when starting the server and running it as the root user. You need to read the manual pages about Su because the syntax for running commands as a specified user is changed.

How to start the server

If you have determined the account used to run the server, you can choose how to start the server. You can run the server manually from the command line or automatically during system startup. There are three main methods to start the server:
Call mysqld directly. This is perhaps the smallest command method. Except that mysqld -- help is a useful command (you can use it to find options you can use in other startup methods), I do not intend to discuss it further.
Call the safe_mysqld script. Safe_mysqld tries to determine the location of the server program and data directory, and then calls the server using the option that reflects these locations. Safe_mysqld redirects the server's standard error output to the error file in the data directory and appears as a record. After the server is started, safe_mysqld also monitors
Server and restart it when it crashes. Safe_mysqld is usually used in BSD-style UNIX versions.
If you have started s a f e _ mysqld as root or in a System Startup Program, the error log will be owned by R o t. If you try to call s a f e _ mysqld as a non-privileged user, the error of "ownership denied" may occur. Delete the error file and try again.
Call the mysql. server script. Run the script "s a f e _ mysqld. MySQL. Server" to start the server. This script is recommended when System V is used to start or shut down the system. This system contains directories of scripts called when a machine logs on or exits a specific running level. It can be called using the start or stop parameters to specify whether to start or stop the server.
The safe_mysqld script is installed in the bin directory of the MySQL installation directory, or in the scripts directory of the MySQL source program distribution package. The MySQL. server script is installed in the s h a r e/MySQL directory of the MySQL installation directory, or in the support-Files directory of the MySQL source program distribution package. If you want to use it
Copy to the appropriate startup directory.
For BSD-style systems, there are several files in the/etc directory that correspond to each other and they start service during boot. The names of these files usually start with 'R C'. Therefore, a file named RC. Local (or similar name) may be generated to start the local installation service. In such a system, you may need to add some rows to the RC as follows. to start the server in the local file (if the path is different from that in your system, you can change it to s a f e _ mysqld ):
If (-x/usr/local/bin/safe_mysqld); then
/Usr/local/bin/safe_mysqld &
Fi
For a system V system, you can install mysql. Server by placing it in the appropriate startup directory under/etc. If you run Linux and install MySQL from the RPM file, this operation may have been completed. Otherwise, you should install the script in the main Startup Script directory and set the connection to it in the appropriate running directory. You can also make the script executable only to the root user.
The layout of the Startup File directory changes with the system, so a comprehensive check is required to figure out how the system organizes them. For example, in linuxppc, these directories are/etc/rc. d/init. d and/e t C/r c. D/r c 3. d. Install the script as follows:
# Cp mysql. Server/etc/rc. d/init. d
# Cd/etc/init. d
# Chmod 500 mysql. Server
# Cd/etc/rc. d/rc3.d
# In-s .. /init. d/MySQL. server s99mysql in Solaris, the main Script directory is/e t C/I n I t. d. Run-level Directory:/e t C/r c 2. d, so the above command will be replaced:
# Cp mysql. Server/etc/init. d
# Cd/etc/init. d
# Chmod 500 mysql. Server
# Cd/etc/rc2.d
# In-S ../init. d/MySQL. Server s99mysql is automatically called by the s99mysql script using the start parameter during system startup.
If you have the chkconfig command (which is commonly used in Linux), you can use it to help install the mysql. server script instead of manually running the preceding command.
1. Specify startup options
When starting a server, if you want to specify an additional startup option, you can use either of the following methods. You can modify the startup script used (safe_mysqld or MySQL. Server) and directly specify these options in the command line that calls the server. You can also specify options in the options file. I suggest you specify the server options in the Global Options file if possible. The file is usually located in/e t C/My. CnF in UNIX and C: My. CnF in Windows (for details about using the option file, see Appendix E ).
Some types of information cannot be specified as server options. For these options, you may need to modify s a f e _ mysqld. For example, if the server cannot correctly pick up the local time zone and the returned Time Value in GMT, you can set the TZ environment variable to give a prompt for this variable. If you use safe_mysqld or MySQL. Ser ver to start the server, you can add the time zone settings to safe_mysqld. Find the command line for starting the server, and add the following command before this line:
TZ = US/Central
Export TZ
This command sets TZ to the US Central Time Zone. Use the appropriate time zone. The syntax is s o l a r I s, and your system may be different. For example, another common syntax for setting TZ variables is:
TZ = cst6cdt
Export TZ
If the startup script is modified, the modifications will be lost the next time you install MySQL (for example, upgrade to the latest version), unless you have copied the startup script elsewhere. After the new version is installed, compare your script with the newly installed script to see what changes need to be made to the new build.
2. Checklist during startup
In addition to arranging server startup during system boot, you can also install a script to run mysamchk and I s a M C H K to check the table before the server starts. You may want to restart after the server crashes, but the table may have been destroyed. Check these tables before starting the server. Chapter 13th contains details about writing and installing such scripts.

Disable the server

To manually shut down the server, you can use mysqla D m I n:
% Mysqladmin Shutdown
To automatically shut down the server, you do not need to perform any special operations. The BSD system usually disables the service by sending a term signal to the process. The process may respond to it or be canceled at will. When mysqld receives a signal, it will terminate the response. For MySQL. server to start the System V-style system of the server. The shutdown process will call the script with the Stop parameter to instruct the server to shut down-of course, it is assumed that you have installed MySQL. server.

Reclaim Server Control when no connection is established

In some environments, because you cannot connect to the server, you need to manually restart it. Of course, this is a bit ridiculous, because it is usually manually disabled by connecting to the server and telling the server to terminate. So how does this happen?
First, the root password of MySQL may obtain a value that you do not know. This may occur when a password is changed-for example, if an invisible control character is entered when a new password value is entered. It is also possible that the password is completely forgotten.
Second, the localhost connection is usually performed through a socket file in the Unix domain, which is generally/T m p/MySQL. s o c k. If the socket file is deleted, the local client cannot connect. If the system occasionally runs a cron job to delete temporary files in/tmp, this may happen.
If you cannot connect because the socket file is lost, you can simply restart the server to recover it, because the server re-establishes the file during startup. It should be noted that you cannot establish a connection with this socket (because it does not exist) and you must establish a TCP/IP connection. For example, if the server host is pit-Viper. S n a k e. n e t, you can connect to the server as follows:
% Mysqladmin-p-uroot-H pit-viper.snake.net Shutdown
If this socket file is deleted by a cron job, the problem will recur until you modify the cron job or use another socket file. You can use the global options file to specify another socket file. For example, if the data directory is/us r/L o c a l/v a r, you can add the following lines to/e t C/My. CNF to move the socket file to it:
[Mysqld]
Socket =/usr/local/var/MySQL. Sock
[Client]
Socket =/usr/local/var/MySQL. Sock
The path name is specified by the server and the client program so that they can use the same socket file. If you only set the path name for the server, the client will still find the socket file in the old location. After this modification, restart the server to create a socket file in a new location.
If you forget the root password or change it to a value you do not know and cannot connect, You need to revoke the control of the server to reset the password:
Disable the server. If you log on to the server host as the root user, you can use the kill command to terminate the server. You can find the ID process of the server by using the ps command or by viewing the server's PID file (usually in the data directory.
It is best to first try to use the standard kill command to cancel the server. This Command sends a term signal to the server to check whether the server responds by closing the signal. That is to say, the table and log will be properly refreshed. If the server is blocked and does not respond to a normal termination signal, use kill-9 to forcibly terminate it. This is the last
Method, because there may be unrefreshed changes, and you have to bear the risk of retaining the table in an inconsistent state. If kill-9 is used to terminate the server, make sure that the tables myisamchk and I s a M C H K are checked before the server is restarted (See Chapter 13th)
Use the -- skip-grant-tables option to restart the server. This operation tells the server not to use an authorized table to check the connection. This allows you to connect as a root user without entering a password. After the connection, modify the RO o t password.
Tell the server to use mysqladmin flush-privileges to start with the authorization table. If your mysqladmin version does not recognize f l us H-priv I l e g E S, try to reload 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.