MySQL server start and stop _ MySQL

Source: Internet
Author: User
Tags unix domain socket
1. run the MySQL server as a non-privileged user. before discussing how to start the MySQL server, let's consider the user identity to run the MySQL server. The server can be started manually or automatically. If you manually start it, the server is started as the user you log on to Unix (Linux). That is, if you log on to Unix with paul and start the server, it runs with paul; 1. run the MySQL server as a non-privileged user
Before discussing how to start the MySQL server, let's take a look at the user identity to run the MySQL server. The server can be started manually or automatically. If you manually start it, the server is started as the user you log on to Unix (Linux). That is, if you log on to Unix with paul and start the server, it runs with paul; if you use the su command to switch to the root, and then run the server, it runs as root. However, in most cases, you may not want to manually start the server. most likely, you have to schedule the MySQL server to be automatically started during system boot, as part of the standard boot process, in Unix, this pilot process is run by the system's Unix user root, and any processes running in this process are run with the root permission.
  
You should keep in mind the two goals of the MySQL server startup process:
  
You want the server to run as a non-root user. Generally, you want to limit the capabilities of any running process unless you really need the root permission, which is not required by MySQL.
It is inconvenient for you to make the server run with the same user at all times, this causes the file and directory to be created under a data directory with different owners, and may cause the server to be unable to access the database or table, depending on which user you are running. You can avoid this problem by using the same user to run the server.
  
   To run the server as a normal non-privileged user, follow these steps:
Select a user for running the server. mysqld can be run by any user. However, it is clear in concept that a separate user is created for MySQL operations. You can also select a user group for MySQL. This document uses mysqladm and mysqlgrp as user names and user group names respectively.
If you have installed MySQL under your account and do not have special management permissions on the system, you may run the server under your own user ID. In this case, replace mysqladm and mysqlgrp with your login name and user group.
If you use the rpm file to install MySQL on RedHat Linux, this installation will automatically create an account named mysql and replace mysqladm with this account.
If necessary, use the system's usual process of creating a user to create a server account, you need to use root for it.
If the server is running, stop it.
Modify the data directory and the owner of any subdirectories and files so that mysqladm users can have them. For example, if the data directory is/usr/local/var, you can set the owner of mysqladm as follows (you need to run these commands as root ):
  
# Cd/usr/local/var
# Chown-R mysqladm. mysqlgrp
  
Modify the permissions of data directories and any subdirectories and files so that they can only be accessed by mysqladm users. If the data directory is/usr/local/var, you can set anything owned by mysqladm:
  
# Cd/usr/local/var
# Chmod-R go-rwx
When setting the owner and mode of the data directory and its content, pay attention to the symbolic connection. You need to follow them and change the owner and mode of the files or directories they direct. If the Directory of the connection file does not belong to you, it may be troublesome. you may need the root identity.
  
After completing the preceding process, make sure that the server is always started during mysqladm or root logon. in the latter, specify the -- user = mysqladm option, enables the server to switch its user ID to mysqladm (and also applies to the system startup process ).
  
-- The user option is introduced in MySQL 3.22. if you have an old version, you can use the su command to tell the system to run the server under a specific user during the root operation.
  
   II. how to start the server
After we confirm the account used to run the server, you can choose how to schedule the server to start. You can run it manually or automatically during system boot from the command line. There are three main methods for starting a server:
  
Call mysqld directly.
This may be the least commonly used method. we recommend that you do not use it more. Therefore, this article will not detail it.
Call the safe_mysqld script.
Safe_mysqld tries to determine the location of the server program and data directory. Then, call the server with the option that reflects these values. Safe_mysqld migrates a standard error device from the server to an error file in the data directory, so that it has a record. After the server is started, safe_mysqld also monitors it and restarts it if it dies. Safe_mysqld is often used in BSD Unix systems.
If you start sqfe_mysqld as root or during system boot, the error log is owned by root, this may cause a "permission denied" error when you attempt to call safe_mysqld with a non-authorized user in the future. delete the error log and try again.
Call the mysql. server script.
This script is intentionally used to start and stop the safe_mysqld.mysql.server on the System V to start the server, the system contains several script directories that are used when the machine enters or exits a given running level. It can use a start or stop parameter to indicate whether you want to start or stop the server.
The safe_mysqld script is installed in the bin directory of the MySQL installation directory, or can be found in the scripts directory of the MySQL source code distribution. The mysql. server script is installed in the share/mysqld directory under the MySQL installation directory or in the support_files Directory distributed by MySQL source code. If you want to use them, you need to copy them to the appropriate directory.
  
For BSD-style systems (FreeBSD, OpenBSD, etc.), there are usually several files in the/etc directory that initialize the service during boot. these files usually have names starting with "rc, and it may be named "rc. local file (or something similar), specifically used to start locally installed services. On such a system, you may add rows similar to the following to the rc. local file to start the server (if the directory of safe_mysqld is different from that of your system, modify it ):
  
If [-x/usr/local/bin/safe_mysqld]; then
/Usr/local/bin/safe_mysqld &
Fi
For a System V System, you can install mysql. server in the appropriate Startup directory under/etc. If you run Linux and install MySQL from an RPM file, you have done this for you. otherwise, install the script in the main Startup directory, and place the connection pointing to it in the appropriate running level Directory. You can also enable the script to be started only by the root user.
  
The directory layout of startup files varies with systems, so you need to check how your system organizes them. For example, on Linux PPC, the directory is/etc/rc. d and/etc/rc. d/rc3.d. you can install the script like this:
  
# Cp mysql. server/etc/rc. d/init. d
# Cd/etc/init. d
# Chmod 500 mysql. server
# Cd/etc/rc. d/rc3.d
# Ln-s ../init. d/mysql. server S99mysql
On solaris, the main script directory is/etc/init. d, and the run-level directory is/etc/rd2.d, so the command looks like this:
  
# Cp mysql. server/etc/rc. d/init. d
# Cd/etc/init. d
# Chmod 500 mysql. server
# Cd/etc/rc2.d
# Ln-s ../init. d/mysql. server S99mysql
When the system starts, the S99mysql script is automatically called with a start parameter. If you have the chkconfig command (available on Linux), you can help install the mysql. server script instead of manually running the command as above.
  
2.1 specify startup options
If you want to specify additional startup options when the server is started, you can use either of the following methods. You can modify the startup script (safe_mysqld or mysql. server) you are using and specify options directly on the line that calls the server, or in an option file. We recommend that you specify an option in a global options file, which is usually located in/etc/my. cnf (Unix) or c: \ my. cnf (Windows ).
  
Some types of information cannot be specified with server options. You may need to modify safe_mysqld. For example, if your server cannot correctly select the local time zone and returns the time value in GMT, you can set the TZ environment variable to give it an indication. If you use safe_mysqld or mysql. server to start the server, you can add a time zone to safe_mysqld. Find the line for starting the server and add the following command before the line:
  
TZ = US/Central
Export TZ
The above command syntax is Solaris. for other system syntaxes may be different, please refer to the relevant Manual. If you have modified your startup script, remember that the next time you install MySQL (such as upgrading to a new version), your modifications will be lost unless you have copied the startup script elsewhere. After the new version is installed, compare the scripts of the new and old versions to see what changes need to be rebuilt.
  
2.2 Check your table at startup
In addition to arranging that your server be started during system boot, you may need to install the myisamchk and isamchk scripts to check your table before the server is started. You may restart after a crash, and the table may have been damaged. it is a good way to detect the problem before starting.
  
   3. stop the server
To manually start the server, use mysqladmin:
  
% Mysqladmin shutdown
  
To automatically stop the server, you do not need to do anything special. The BSD system generally stops the service by sending a TERM signal to the process. they either respond to it correctly or are roughly killed. When it receives this signal, mysqld uses termination as the response. For a System V-style System that uses mysql. server to start the server, the stop process uses a stop parameter to call the script and tell the server to terminate the process. assume that you have installed mysql. server.
  
   4. if you cannot connect to the server, how can you regain control over the server?
In some cases, you may manually restart the server because you cannot connect to it. Of course, this is a bit contradictory. Because you usually turn it off manually by connecting to the server, how can this happen.
  
First, the MySQL root password can be set to a value that you do not know. This may happen when you change the password, for example, if you accidentally enter an invisible control character when entering a new password. You may also forget the password.
  
Second, connecting to localhost is usually done through a Unix domain socket file, usually/tmp/mysql. sock. If the socket file is deleted, the local client cannot connect. This may occur when your system runs a cron task to delete temporary files under/tmp.
  
If you cannot connect because of the loss of the socket file, you can simply restart the server and recreate it. Because the server re-creates it at startup. The scam here is that you cannot establish a connection with a socket because it is gone, you must establish a TCP/IP connection, for example, if the server host is pit.snke.net, you can connect like this:
  
% Mysqladmin-p-u root-h pit.snke.net shutdown
  
If the socket file is deleted by a cron task, the problem will occur again. unless you modify the cron task or use one or more different socket files, you can use the global option file.

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.