Start and Stop a MySQL Server

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 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 is started as a user logging 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, however

Then run the server as root. However, in most cases, you may not want to manually start the server. The most likely reason is that you want the MySQL server to be automatically started during system boot,

As part of the standard boot process, in Unix, the boot process is executed by the system's Unix user root, and any processes running in the process are run as root.
  
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.
If you want the server to run with the same user all the time, it is inconvenient to use one user and run the server with another different user at other times, which makes it a file and directory

It is created in a data directory with different owners, and may cause the server to be unable to access the database or table. This depends on the user you run. You can avoid using the same user to run the server.

Avoid this problem.
  
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

MySQL selects a user group. 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

Your login name and user group replace mysqladm and mysqlgrp.
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 mys as follows:

Owner of qladm (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 you have:
  
# 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 connection file

The directory does not belong to you, which may cause some trouble. You may need the root identity.
  
After completing the above process, you should ensure that the server is always started at mysqladm or root login. In the latter, make sure to specify the -- user = mysqladm option so that the server can

Its user ID switches 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. For

There are three main methods to start 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 relocate devices with standard errors from the server

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 commonly used in B

SD Unix system.
If you start sqfe_mysqld as root or during system boot, the error log is owned by root, which may be exported when you attempt to use a non-privileged user to call safe_mysqld.

"Permission denied" error. Delete the error log and try again.
Call the mysql. server script.
This script is used to start and stop the safe_mysqld.mysql.server on the System for System V. The System contains several

A script directory used for vertices at 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. Install mysql. server script in MySQL

Under the share/mysqld directory or under the support_files directory that MySQL source code distributes. 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 a file named "rc. local" (or something similar) that is specially used to start locally installed services. In such a system, you may add rows similar to the following

C. Start the server in the local file (if the directory of safe_mysqld is different in 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

Install MySQL. Otherwise, install the script in the main startup directory and place the connection pointing to it in the appropriate running directory. You can also make the script

Start.
  
The directory layout of startup files varies with systems, so you need to check how your system organizes them. For example, in Linux

On PPC, the directory is/etc/rc. d and/etc/rc. d/rc3.d, so that 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 preceding 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 you are using (safe_mysqld or mysql. server) and directly

Specify options on the line of the server or in an option file. We recommend that you specify the option in a global option 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 value in GMT

Return time value. 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 to install MySQL next time.

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 needs 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 be duplicate after a crash

The table may have been damaged. It is a good way to detect the problem before starting the table.
  
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. Mysq

When it receives this signal, the ld 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 this script.

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. This is because you usually turn it off manually by connecting to the server.

How it appears.
  
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 characters. 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

You cannot establish a connection with a socket because

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.