MySQL server startup and shutdown

Source: Internet
Author: User
Tags chmod command line connect mysql mysql in socket root access linux
mysql| server as a MySQL administrator, a common goal is to ensure that the server is as running as possible, so that the client can access it at any time. However, it is sometimes best to shut down the server (for example, if you are relocating a database, you do not want the server to update the table in that database). The requirement relationship between keeping the server running and occasionally shutting it down is not the book's solution
It's a decision. But we can at least discuss how to start and stop the server so that you have the ability to do both.
The instructions in this chapter are for UNIX systems only. If you are running a Windows system, you can skip this chapter because all required startup and shutdown commands are included in Appendix A, "Getting and installing software."
Invoke the command given in this chapter
For simplicity, in most cases, programs such as Mysqla d m i n, mysqldump, and so on, do not give any-H,-u, or-P options in this chapter. I assume that you will invoke these programs with whatever options you need to connect to the server.

Running MySQL server with no privileged user account

Before discussing how to start the server, consider which account you should run when the server starts. The server can be started manually and automatically. If it is started manually, the server runs as a UNIX user (you are registered as that user). That is, if the author registers with Paul and starts the server, it will run as Paul. If you switch the user to root and then start the server with the S U command, the server runs as root.
However, most of the time it is not possible to use a manual boot server. It is very likely that you will schedule the server to run automatically as part of the standard boot process when the system boots. In Unix, the boot process is performed by the system as a UNIX R o T user, and any processes that are started in the process are run with root permissions.
You should keep in mind the two goals of the MySQL server startup process:
To start the server as some non-root user. Typically, you should limit the ability of any process unless the process really needs root access and MySQL cannot do so.
You want the server to always run as the same user. The server sometimes runs as a user and sometimes runs as a different user, creating contradictions. This will cause files and directories to be created under different ownership under that data, and even cause the server to not be able to access the database or tables. Running the server consistently as the same user can avoid this problem.
To run the database as a standard, unprivileged user, you can perform this procedure as follows:
1 Select the account that is used to run the server. Mysqld can run as any user, but it is clear that it creates a separate account for MySQL activities only. You can also specify a group specifically for MySQL. The authors name the names of 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 MySQL installed under your account and you do not have specific administrative rights in the system, you can run the server under your own ID user. In this case, you should use your own registration name and group name instead of Mysqladm and MYSQLGRP.
If you use the rpm file to install MySQL under Redhat Linux, the installer will automatically create an account under MySQL name. You should replace Mysqladm with this name.
2 if necessary, use the system commonly used account creation process (a C count-c r e a t i o N) to create the server account. This needs to be done as root.
3 Shut down the server (if it is running).
4 Modify the Data directory and ownership of any subdirectories and files so that mysqladm users have them. For example, if the data directory is/US r/l o c a l/v a R, you can mysqladm the user's ownership by following these settings:
# Cd/usr/local/var move to Data directory
# Chown-r MYSQLADMIN.MYSQLGRP Set ownership of all directories and files
5 Modify the Data directory and the permissions of any subdirectories and files so that only mysqladm users can access them. Setting this method to prevent other people from accessing is a good security precaution. If the data directory is/US r/l o c a l/v a R, you can set everything you want by mysqladm users by following these actions (you need to run these commands as root):
# Cd/usr/local/var move to Data directory
# Chmod-r go-rwx make everything accessible only to Mysqladm
Observe symbolic connections when setting the ownership and manner of the data directory and its contents. You need to track symbolic connections and modify the ownership and manner of the files or directories that you point to. If these connection files are located in a directory that does not belong to you, doing so may cause trouble, so you must be the root user.
After you complete the preceding procedure, make sure that you always start the server either as a mysqladm or as a root user. In the latter, make sure that the option to--user = Mysqladm is specified so that the server can switch its user ID to Mysqla d m (this option is also available during system startup).
The--user option is added to MySQL in MySQL3.22. If your version is older than MySQL3.22, you can use the SU command to instruct the system to run the server under the specified account when you start the server and run as the root user. You need to read the artificial pages about SU because the syntax for running commands as a specified user is changed.

How to start the server

If you have identified the account that is used to run the server, you can choose how to schedule the server to start. You can run it manually from the command line or run the server automatically during system startup. There are three main ways to start a server:
Call Mysqld directly. This is perhaps the smallest command method. In addition to stating that mysqld--help is a useful command (with which you can find options that you can use with other startup methods), the author does not intend to discuss it further.
Invokes the Safe_mysqld script. Safe_mysqld attempts to determine the location of the server program and data directory, and then invokes the server with options that reflect 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 starting the server, SAFE_MYSQLD also monitors
Server and reboot when it is dead. Safe_mysqld is typically used in the BSD-style version of UNIX.
If you have started the s a f E _ mysqld as root or in the 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, you may get a "ownership denied" error. Delete the error file and try again. Invokes the Mysql.server script. By running S A f e _ mysqld. Mysql. Server, the script starts the servers. This script is recommended for use in systems with system V boot/shutdown systems. This system includes several directories containing scripts that are invoked when a machine logs on or exits a specific run level. It can be invoked using the start or stop arguments to indicate whether you want to start or shut down 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 distribution package. The Mysql.server script is installed in the E/mysql directory of the MySQL installation directory or in the Support-files directory of the MySQL source distribution package. If you want to use it, you should
Copy to the appropriate startup directory.
For BSD-style systems, there are several files in the/etc directory that correspond, and they start the service during the boot period. The names of these files usually start with ' r C ', so it is likely that a file named Rc.local (or a similar name) will be used to start the local installation service. In such a system, you might want to add some rows to the Rc.local file to start the server (if the path is different from your system, you can modify it to s a f E _ Mysqld):
if (-x/usr/local/bin/safe_mysqld); then
/usr/local/bin/safe_mysqld &
Fi
For system V-style systems, you can install MySQL by placing it in the appropriate startup directory under/ETC. Server If you run Linux and install MySQL from the rpm file, this may be done. Otherwise, you should install the script in the Master Boot script directory and set up a connection to it in the appropriate run-level directory. You can also make the script executable only for root users.
The layout of the boot file directory changes with the system, so you will need a comprehensive check 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. You should install the script as follows:
# CP MYSQL.SERVER/ETC/RC.D/INIT.D
# CD/ETC/INIT.D
# chmod 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 for/e t c/r C 2. D, so the above command will be replaced by:
#cp MYSQL.SERVER/ETC/INIT.D
# CD/ETC/INIT.D
# chmod Mysql.server
# CD/ETC/RC2.D
# in-s. /init.d/mysql.server S99mysql during system startup, the S99mysql script is invoked automatically using the start parameter.
If you have the Chkconfig command (which is common in Linux), you can use it to help install the Mysql.server script instead of manually running the above command.
1. Specify startup options
If you want to specify additional startup options when starting the server, you can do so in two different ways. You can modify the startup script that you are using (safe_mysqld or MySQL. Server) and specify these options directly in the command line of the calling server. You can also specify options in the options file. The author recommends that you specify the server option in the global options file if possible. Typically, the location of this file is in/E T c/my.cnf in Unix and c:\my.cnf in Windows (see Appendix E for details on using the option file).
Certain kinds of information cannot be specified as options for the server. For these options, you may need to modify s a f e _ mysqld. For example, if the server does not properly pick up the local time zone (zone) and return time values in GMT, you can set the TZ environment variable to give the variable a hint. If you use Safe_mysqld or MySQL. Ser ver start the server, you can add the time zone setting to Safe_mysqld. Locate the command line that starts the server, and add the following command before the line:
Tz=us/central
Export TZ
This command sets TZ to the US-centered time zone. You need to use a time zone of the appropriate location. 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 you modify the startup script, the next time you install MySQL (for example, upgrade to an updated version), you will lose these modifications unless you have previously copied the startup script to another location. After you install the new version, compare your script with the newly installed script to see what changes are needed to re-establish it.
2. Check the table during startup
In addition to scheduling the server to boot at system boot, you can install a script to run Mysamchk and I s a M c h k to check the table before the server starts. You may intend to reboot after the server crashes, but the table may be corrupted. Checking these tables before the server starts is a good way to find the problem. The 13th chapter contains details about writing and installing such scripts.

Shutting down the server

To manually turn off the server, you can use Mysqla d m i n:
% mysqladmin shutdown
You do not need to do anything special to shut down the server automatically. BSD systems typically turn off services by sending a term signal to the process. Process or respond to it, or be arbitrarily canceled. When Mysqld receives a signal, it responds by terminating it. For System V-style systems that use Mysql.server to start the server, the shutdown process calls the script with the stop parameter to instruct the server to shut down--which is, of course, assuming you have MySQL installed. Ser ver of the case.

Reclaim the control of the server when not connected

In some environments, because you cannot connect to the server, you need to restart it manually. Of course, this is a bit absurd, because it is usually done by manually shutting down the server by connecting to the server and telling the server to terminate. So how did this happen?
First, MySQL's root password may have a value that you don't know about. This may happen when a password is modified-for example, if you happen to type an invisible control character when you enter a new password value. It is also possible to completely forget the password.
Second, the connection to the localhost is usually done through a socket file in the UNIX domain, which is typically/T M p/mysql. S o C K. If the socket file is deleted, the local client computer will not be able to connect. This can happen if the system occasionally runs a cron job that deletes temporary files in/tmp.
If you cannot connect because a socket file is lost, you can simply recover by restarting the server because the server has been able to re-establish the file during startup. What you should know here is that you cannot establish a connection with this socket (because it no longer exists) and you must establish a TCP/IP connection. For example, if the host of the server is Pit-viper. s n A k E. N e T, you can connect 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 specify another socket file with the global options file. For example, if the data directory is/US r/l o c a l/v a R, you can move the socket file there by adding the following line to/E T c/my.cnf:
[Mysqld]
Socket=/usr/local/var/mysql.sock
[Client]
Socket=/usr/local/var/mysql.sock
The path name is specified for both the server and the client program so that they can use the same socket file. If you set the path name only on the server, the client program will still look for the socket file in the old location. After making this change, you should restart the server so that it creates a socket file in the new location.
If you cannot connect because you have forgotten the password for root or modified it to a value that you do not know, you will need to retract the control of the server to reset the password:
Shut down the server. If you are logged on as the root user on the server host, you can terminate the server with the KILL command. You can find the server's ID process by using the PS command or by looking at the server's PID file (usually in the data directory).
It is best to try to cancel the server with the standard KILL command, which sends a term signal to the server to see if the server responds by shutting down the signal. In other words, the tables and logs will be refreshed appropriately. If the server is blocked and does not respond to a normal termination signal, use kill-9 to force it to terminate. This is the last one.
method, because there may be a change that has not been refreshed, and a risk that the table will be held in an inconsistent state. If you terminate the server with kill-9, make sure that the table is checked using myisamchk and I s a M c h K before restarting the server (see chap. 13th)
Restart the server with the--skip-grant-tables option. This action tells the server not to use the authorized table to check the connection. This allows you to connect as the root user without entering a password. After the connection, modify the password for R o T.
Tell the server to use the Mysqladmin flush-privileges to start using the authorization table. If your mysqladmin version does not recognize F l US h-priv l e g e S, try reloading.

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.