MySQL start stop, a server runs multiple MySQL databases

Source: Internet
Author: User
Tags chmod local time unix domain socket

One, running the MySQL server as a non-privileged user
Before discussing how to start the MySQL server, let's consider the user who should run the MySQL service
Manager The server can be started manually or automatically. If you manually start it, the server is used to log you in to UNIX (Linux)
If you use Paul to log in to UNIX and start the server, it will run with Paul; If you switch with the SU command
To root, and then transport starts the server, it runs as root. However, in most cases you may not want to start the service manually
Most likely you are scheduling the MySQL server to start automatically at System boot, as part of the standard boot process,
Under UNIX, the boot process is performed by the UNIX user root of the system, and any processes running in the process are
Root permission to run.

You should keep in mind the two goals of the MySQL server startup process:
1. You want to have the server run as a non-root user. In general, you want to limit the ability of any running process, except
Root permissions are not required, and MySQL is not required.
2. You want the server to always run with the same user, at this time with one user and other time with a different
It is inconvenient for the user to run the server, which causes the data directory to have a different owner for the files and directories
and may cause the server to be unable to access the database or table, which depends on which user you are running. Unified
Running the server with the same user is the problem you avoid.

To run the server with a normal, non-privileged user, follow these steps:
1. Select a user to run the server, mysqld can run with any user. But conceptually more clearly.
is to create a separate user for the MySQL operation. You can also select a user group specifically for MySQL. This
The text uses Mysqladm and MYSQLGRP respectively as user name and user group name.
2. If you have installed MySQL under your account and do not have special administrative privileges on the system, you may
Run the server under your own user ID. In this case, use your own login name and user group instead
Mysqladm and Mysqlgrp.
3. If you install MySQL with rpm files on Redhat Linux, the installation will automatically create a mysql-named
Account, use this account instead of Mysqladm.
4. If necessary, create a server account with the system's usual process of creating a user, you need to do it with root.
5. If the server is running, stop it.
6. Modify the owner of the data directory and any subdirectories and files so that mysqladm users have them. For example
If the data directory is/usr/local/var, you can set the owner of the Mysqladm as follows (you need to root
Run these commands):
#cd/usr/local/var
#chown-R MYSQLADM.MYSQLGRP

Modify the permissions of the data directory and any subdirectories and files so that they can only be accessed by mysqladm users. If the number
According to the directory is/usr/local/var, you can set anything owned by Mysqladm:
# Cd/usr/local/var
# Chmod-r GO-RWX
Note the symbolic connection when you set the owner and mode of the data directory and its contents. You need to go along and change it.
The owner and mode of the file or directory that they point to. If the directory of connected files is located in a place that does not belong to you, there will be some trouble
You may need root status.
After you have completed the process, you should ensure that you always start the server when you log on with mysqladm or root,
Specify the--USER=MYSQLADM option so that the server can switch its user ID to Mysqladm (also applicable
During the system boot process).
The--user option is introduced in MySQL 3.22, and if you have an older version, you can use the SU command to tell the system to
The root runtime runs the server under a specific user.

Second, the method of starting the server
After we have identified the account used to run the server, you can choose how to schedule the server to start. You can obey.
Make the line run manually or automatically during system boot. There are three primary methods for starting the server:
1. Call mysqld directly.
This is probably the least common method and is not recommended to use much, so this article is not detailed.
2. Call the Safe_mysqld script.
Safe_mysqld attempts to determine the location of the server program and data directory. Then use the options that reflect these values to adjust
With the server. Safe_mysqld to relocate a standard error device from the server to an error file in the data directory,
Makes a record of it. After starting the server, SAFE_MYSQLD also monitors it and restarts if it dies
It. Safe_mysqld is commonly used in BSD-style UNIX systems.
If you start sqfe_mysqld as root or during system boot, the error log is owned by Root, which may
When you try to call Safe_mysqld with a non-privileged user later, you will cause "Permission Denied" (Permissions
Reject) error, delete the error log and try again.
3. Call the Mysql.server script.
This script is intended to be used by safe_mysqld.mysql.server on System V start and stop systems
To start the server, which consists of a few feet that are used when the machine enters or exits a given run level.
This directory. 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 distributed in the MySQL source code
Found under the Scripts directory. Mysql.server scripts are installed in the Share/mysqld directory under the MySQL installation directory
Or it can be found under the Support_files directory of MySQL source code distribution. If you want to use them, you need to
They are copied to the appropriate directory.
For BSD-style systems (FREEBSD,OPENBSD, etc.), there are usually several files in the/etc directory that boot
When initializing the service, these files usually have a name that starts with "RC", and it may be called "rc.local"
File (or something like that) that was deliberately used to start a locally installed service. On such a system, you may be
A line similar to the following is added to the rc.local file to start the server (if the Safe_mysqld directory is in your department
Change it to a different system):
if [-x/usr/local/bin/safe_mysqld]; Then
/usr/local/bin/safe_mysqld &
Fi

For System V-style systems, you can start them properly by placing mysql.server in/etc
Directory down to install it. If you run Linux and install MySQL from a rpm file, these are ready for you,
Otherwise, install the script in the main startup directory and place the connection that points to it under the appropriate RunLevel directory. You also
Allows the script to be started only by Root.

The directory layout of the startup files is different for each system, so you need to check to see how your system organizes them.
For example, on Linux PPC, directories are/ETC/RC.D and/ETC/RC.D/RC3.D, so you can install scripts like this:

#cp MYSQL.SERVER/ETC/RC.D/INIT.D
#cd/ETC/INIT.D
#chmod 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 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 chkconfig life,
(available on Linux), you can help install the Mysql.server script instead of running it manually as above
The above command.

2.1 Specifying startup options
If you want to specify additional startup options when the server starts, you can do so in two ways. You can modify you to make
(Safe_mysqld or Mysql.server) and specify options directly on the row of the calling server,
or specify options in one of the options files. It is recommended that if you specify an option in a global options file, it will pass
Often located in/etc/my.cnf (Unix) or c:my.cnf (Windows).

Some kinds of information cannot be specified with the server option. For these you may need to modify safe_mysqld.
For example, if your server does not correctly select the local time zone and returns the time value in GMT, you can set the TZ environment
Variable gives it an indication. If you start the server with Safe_mysqld or mysql.server, you can add a
The time zone setting is added to Safe_mysqld. Locate the row that started the server and add the following command before the row:

Tz=us/central
Export TZ
The syntax for the above command is Solaris and may be different for other system syntax, please consult the relevant manual. Such as
If you do change your startup script, remember that the next time you install MySQL (such as upgrading to a new version), your
The modification will be lost unless you first copy the startup script elsewhere. After you install the new version, compare the old and new versions of the
Script to see what changes you need to rebuild.

2.2 Check your table at startup
In addition to scheduling your server to boot at system boot, you may want to install the MYISAMCHK and Isamchk scripts,
To check your table before the server starts. You may restart after a crash, there may be a table that has been compromised in the boot
Check it before it is a good way to find problems.

Third, stop the server
To manually stop the server, use Mysqladmin:
Mysqladmin shutdown
To stop the server automatically, you don't need to do anything special. BSD systems typically stop by sending a term signal to the process
The service, they either answer it correctly or are brutally killed. The mysqld is terminated as a response when it receives the signal.
For System V-style systems that start the server with Mysql.server, the stop process will call that with a stop parameter
Script that tells the server to terminate, of course, assuming you have installed mysql.server.

Iv. how to regain control of the server if you cannot connect to 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 a
You turn it off manually by connecting to the server, so how does that happen.
First, the MySQL root password can be set to a value you do not know, which may occur when you change the password,
For example, if you accidentally type a control character that is not visible when you enter a new password. You may also forget your password.
Second, the connection localhost is usually done through a UNIX domain socket file, typically/tmp/mysql.sock.
If the socket file is deleted, the local client cannot connect. This can happen on your system running a cron task
The temp file under/TMP was deleted.
If you are unable to connect because of a missing socket file, you can simply recreate it by restarting the server.
Because the server re-creates it at startup. The trick here is you can't connect with sockets because it's gone, you
You must establish a TCP/IP connection, for example, if the server host is pit.snake.net, you can connect this way:
%mysqladmin-p-u root-h pit.snake.net shutdown
If a socket file is deleted by a cron task, the problem recurs unless you modify the cron task or use a
Or use a different socket file, you can use the global options file to specify a different socket, for example,
If the data directory is/usr/local/var, you can add the socket file by adding the following line to the/ETC/MY.CNF
To move there:
[Mysqld]
Socket=/usr/local/var/mysql.sock
[Client]
Socket=/usr/local/var/mysql.sock

Path names are specified for both the server and the client so that they all use the same socket file. If you are only for server
Set the path, the client will still expect the socket to be executed in its original location, restart the server after modification, and make it in the new location
Creates a socket.
If you cannot connect because you have forgotten the root password or have set it to a different value than you think, you need to regain
To control the server, you can set the password again:
Interrupt Server
If you log on to the server host as root, you can terminate the server with the KILL command. You can use the PS command or the
Search the server's PID file (usually in the data directory) to find out the ID of the server process.
It is best to first attempt to use a normal kill to send a term signal to the server to see if it will terminate normally
For. In this way, the tables and logs will be emptied correctly. If the server is blocked and does not answer a normal termination signal,
You can force it to terminate with kill-9. This is the last resort, as this may have an unmodified modification, and you risk
The risk of having the table in an inconsistent state.
If you use Kill-9 to terminate the server, be sure to check your myisamchk and Isamchk before starting the server.
Table.
Restart the server with the--skip-grant-table option.
This tells the server not to use the authorization table to authenticate the connection, which allows you to connect as root without a password. After you have connected
, change the root password.
Use Mysqladmin flush-privileges to tell the server to start again with the authorization table
If your mysqladmin version does not know Flash-privileges, try reload.

Five. Running multiple servers
Most of them run a single MySQL server on a given machine, but in many cases, running multiple servers
is very useful:
You may want to test a new version of a server and keep the production server you are running. In this case,
You will run a different server code.
The operating system generally restricts the number of open file handles per process. If your system is difficult to increase this limit, the shipping
Row multiple servers is one way to resolve the limitations. In this scenario, you may run multiple instances of the unified server.
ISPs often provide their customers with their own MySQL installation, and it is necessary to involve separate servers. In this case, you
You may run multiple instances of the same version or different versions if different customers want different versions of MySQL.
Naturally, it is much more complex to run multiple servers than to run only one server. If you install multiple versions, you
You can't install everything in the same place. When the server is running, some parameters must or are most likely for each server to be
Unique, they include where the server is installed, the pathname of its data directory, the TCP/IP port, and the UNIX domain socket path
Name and the UNIX account used to run the server (if you are no longer running all servers under the same account). If you decide
To run multiple servers, be sure to note the parameters you use, and that you don't lose track of what's going on.

5.1 Configuring and installing multiple servers
If you want to run different versions of the server instead of multiple instances of the same version, you must be in a different location
Install them. If you install binary distribution (without RPM), they will be installed in directory names with different version numbers
Under If you install from source code, the simplest way is to run configure configuration MySQL installed in each version
Using the--with-prefix option in the process allows different distributions to be separated, which will allow everything to be installed in a separate
directory, you can link the directory domain distribution version number, for example, you can configure a MySQL distribution like this,
Where version is the MySQL release number:
%.configure--with-prefix=/usr/local/mysql-version
The--with-prefix option also determines a unique data directory for the server. You may want to add other services
Specific options, such as the TCP/IP port number and socket pathname (--with-tcp-port and--with-unix-socket).
If you want to run multiple instances of the same version server, any option that must be based on a server-specific setting
The item will need to be specified at run time.

5.2 Multi-server boot process
Starting multiple servers is more complex than using a single server. Because Safe_mysqld and Mysql.server are both in
The single server setup works best. I suggest you study safe_mysqld and use it as your starting
process, using your modified version, you can tailor it more precisely to your own needs.
One problem you have to deal with is how to specify options in the options file (MY.CNF). For multiple servers,
You cannot use/ETC/MY.CNF for each of the different server settings, you can only have the same settings for all servers
Use the file. If the server has a different compiled data directory location, you can
Specify the settings to be used by all servers in MY.CNF in the data directory, and use DATADIR/MY.CNF to specify the service
Specific settings, where the DataDir is different from the server.
Another way to specify server options is to use--default-file=path_name as the first command line
option to tell the server to read from a file named Path_name so you can select a server
Item is placed in a file that is unique to the server, and then tells the server to read the file at startup. Note, such as
If you specify this option, you will not use any one of the usual options files such as/ETC/MY.CNF.

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.