Start and close the MySQL server

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

As a MySQL administrator, a common goal is to ensure that the server is running as much as possible so that the client can access it at any time. However, it is best to disable the server sometimes (for example, if the database is being relocated, you do not want the server to update the table in the database ). This article does not resolve the need to maintain the relationship between server running and occasional shutdown. However, we can at least discuss how to start and stop the server so that you can perform these two operations.

This chapter only applies to UNIX systems.

Call the commands provided in this Chapter

For simplicity, in most cases, programs such as mysqla d m I n and mysqldump do not provide any-h,-u, or-p options 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 the server and restarts 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 it 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 configuring server startup during system boot

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.