In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to start and stop the MySQL server under linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to start and stop the MySQL server under linux".
Running the MySQL server as an unprivileged user
Before we discuss how to start the MySQL server, let's consider as what user we should run the MySQL server. The server can be started manually or automatically. If you start it manually, the server starts as the user you logged in to Unix (Linux), that is, if you log in to Unix with paul and start the server, it runs with paul; if you use the su command to switch to root, and then run to start the server, it runs as root. However, in most cases you may not want to start the server manually, it is most likely that you arrange for the MySQL server to start automatically when the system boots as part of the standard boot process, which is performed by the system's Unix user root under Unix, and any process running during the process runs with root privileges.
You should keep in mind two goals of the MySQL server startup process:
You want the server to run as some non-root user. In general, you want to limit the ability of any process to run, unless you do need root permissions, which MySQL does not.
If you want the server to always run as the same user, it is inconvenient to run the server with one user and another user at other times, which results in the creation of files and directories under data directories with different owners, and may result in the server not being able to access the database or table, depending on which user you run under. You avoid this problem by running the server with the same user.
For an ordinary unprivileged user to run the server, follow these steps:
Select a user to run the server, and mysqld can run with any user. But it is conceptually clear to create a separate user for the MySQL operation. You can also select a user group specifically for MySQL. This article uses mysqladm and mysqlgrp as the user name and user group name, respectively.
If you have installed MySQL under your account and do not have special administrative privileges on the system, you will probably run the server under your own user ID. In this case, replace mysqladm and mysqlgrp with your own login and user group.
If you install MySQL with a rpm file on RedHat Linux, the installation will automatically create an account called mysql instead of mysqladm.
If necessary, use the system's usual process of creating users to create a server account, which you need to do with root.
If the server is running, stop it.
Modify the owner of the data directory and any subdirectories and files so that the mysqladm user owns 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 the data directory 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 you set the ownership and mode of the data catalog and its contents, pay attention to symbolic links. You need to follow them and change the owner and mode of the file or directory they point to. If the directory of the connection file is located in a place that does not belong to you, it will be troublesome and you may need root identity.
After you have completed the above process, you should make sure that the server is always started when logging in as mysqladm or root, and in the latter, be sure to specify the-- user=mysqladm option so that the server can switch its user ID to mysqladm (also applicable to the system startup process).
The user option was introduced in MySQL 3.22. If you have an older version, you can use the su command to tell the system to run the server under a specific user while running as root.
Second, the method of starting the server
After we have determined the account used to run the server, you can choose how to arrange to start the server. You can run it manually or automatically during system boot from the command line. There are three main ways to start the server:
Call mysqld directly.
This is probably the least commonly used method, and it is recommended that you do not use it too much, so this article will not cover it in detail.
Invoke the safe_mysqld script.
Safe_mysqld attempts to determine the location of server programs and data directories. The server is then invoked with options that reflect these values. Safe_mysqld relocates the standard error device from the server to an error file in the data directory so that it has a record. After starting the server, safe_mysqld also monitors it and restarts it if it dies. Safe_mysqld is commonly used in BSD-style Unix systems.
If you start sqfe_mysqld with root or during system boot, the error log is owned by root, which may cause a "permission denied" error when you try to call safe_mysqld with a non-privileged user later. Delete the error log and try again.
Invoke the mysql.server script.
This script starts the server through safe_mysqld.mysql.server on a system intended to start and stop System V, which contains several script directories that are invoked when the machine enters or exits a given run level. It can use a start or stop parameter point 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 where the MySQL source code is distributed. The mysql.server script is installed in the share/mysqld directory under the MySQL installation directory or can be found in the support_files directory where the MySQL source code is distributed. 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 under the / etc directory that initialize the service at boot, these files usually have the name "rc", and it is possible that a file named "rc.local" (or something like that) is specifically used to start the locally installed service. On such a system, you might add a line similar to the following to the rc.local file to start the server (if the safe_mysqld directory is different on your system, modify it):
If [- x / usr/local/bin/safe_mysqld]; then / usr/local/bin/safe_mysqld & fi
For System V-style systems, you can install it by placing mysql.server in the appropriate boot directory under / etc. If you run Linux and install MySQL from a RPM file, this is already done for you, otherwise install the script in the main startup directory and place the connection to it in the appropriate run-level directory. You can also make the script start only by root.
The directory layout of startup files varies from system to system, so you need to check to see how your system organizes them. For example, on Linux PPC, the directories are / etc/rc.d and / etc/rc.d/rc3.d, so 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 use it to help install the mysql.server script instead of running the above command manually as above.
2.1 specify startup options
If you want to specify additional startup options when the server starts, you can do it in two ways. You can modify the startup script you use (safe_mysqld or mysql.server) and specify options directly on the line that invokes the server, or in an options file. It is recommended that you specify options in a global options file if possible, which is usually located in / etc/my.cnf (Unix) or c:\ my.cnf (Windows).
Some types of information cannot be specified as server options. You may need to modify the safe_mysqld for these. For example, if your server does not correctly select the local time zone and returns the time value with GMT, you can set the TZ environment variable to give it an indication. If you start the server with safe_mysqld or mysql.server, you can add a time zone setting to safe_mysqld. Locate the line that starts the server and add the following command before the line:
TZ=US/Centralexport TZ
The syntax of the above command is Solaris, for other system syntax may be different, please refer to the relevant manual. If you do modify your startup script, keep in mind that the next time you install MySQL (such as upgrading to a new version), your changes will be lost unless you first copy the startup script elsewhere. After installing the new version, compare the new version of the script with the old version to see what changes you need to rebuild.
2.2 check your watch at startup
In addition to scheduling your server to start when the system boots, you may want to install myisamchk and isamchk scripts to check your table before the server starts. You may restart after a crash, it is possible that the table has been damaged, and checking it before starting is a good way to find problems.
3. Stop the server
To start the server manually, use mysqladmin:
% mysqladmin shutdown
To stop the server automatically, you don't have to do anything special. BSD systems generally stop service by sending a TERM signal to the process, and they either answer it correctly or are killed rudely. Mysqld responds with termination when it receives this signal. For System V-style systems that start the server with mysql.server, the stop process will call the script with a stop parameter, telling the server to terminate, assuming, of course, that you have mysql.server installed.
If you can't connect to the server, how to regain control of the server
In some cases, you may restart the server manually because you cannot connect to it. Of course, it's a bit of a contradiction. Because you usually turn it off manually by connecting to the server, how does this happen?
First, the MySQL root password can be set to a value you don't know, which may happen when you change the password, for example, if you accidentally type in an invisible control character when entering a new password. You may also forget your password.
Second, the connection to localhost is usually done through a Unix domain socket file, usually / tmp/mysql.sock. If the socket file is deleted, the local customer cannot connect. This may happen when your system runs a cron task to delete temporary files under / tmp.
If you can't connect because you lost the socket file, you can simply recreate it by restarting the server. Because the server recreates it when it starts. The trick here is that you can't make a connection with a socket because it's gone, and you have to establish a TCP/IP connection, for example, if the server host is pit.snake.net, you can connect like this:
% mysqladmin-p-u root-h pit.snake.net shutdown
If the socket file is deleted by a cron task, the problem will repeat unless you modify the cron task or use one or 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 move the socket file there by adding the following line to / etc/my.cnf:
[mysqld] socket=/usr/local/var/ mysql.sock[client] socket=/usr/local/var/mysql.sock
Assign pathnames to both the server and the customer so that they both use the same socket file. If you only set the path for the server, the client program will still expect to execute the socket in the original location and restart the server after modification to create the socket in the new location.
If you cannot connect because you have forgotten the root password or have set it to a different value than you thought, you need to regain control of the server so that you can set the password again:
Interrupt the server
If you log in to the server host as root, you can terminate the server with the kill command. You can use the ps command or find the server process's ID by looking for the server's PID file (usually in the data directory).
It is best to first try to use a normal kill that sends a TERM signal to the server to see if it will terminate the reply normally. In this way, the tables and logs will be emptied correctly. If the server is blocked and does not respond to a normal termination signal, you can force it to terminate with kill-9. This is the last resort, because there may be uncleared changes, and you run the risk of leaving the watch in an inconsistent state.
If you use kill-9 to terminate the server, be sure to check your table with myisamchk and isamchk before starting the server.
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 with root without a password. After you have connected, change the root password.
Use mysqladmin flush-privileges to tell the server to start using the authorization table again
If your version of mysqladmin does not recognize flash-privileges, try reload.
Running multiple servers
Most run a single MySQL server on a given machine, but in many cases, it is useful to run multiple servers:
You may want to test a new version of the server while keeping the production server you are running. In this case, you will run different server code.
The operating system generally limits the number of open file handles per process. If it is difficult for your system to raise this limit, running multiple servers is one way to solve the limitation. In this case, you may be running multiple instances of the unified server.
ISP often provides its customers with their own MySQL installation, and it is necessary to involve separate servers. In this case, you may run multiple instances of the same version or different versions if different customers want different versions of MySQL.
Naturally, running multiple servers is much more complex than running only one server. If you install multiple versions, you can't install everything in the same place. When the server is running, some parameters must or are likely to be unique to each server, including where the server is installed, the pathname of its data directory, TCP/IP port and UNIX domain socket pathname, 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 pay attention to the parameters you use so that you don't lose track of what happened.
5.1 configure and install multiple servers
If you want to run different versions of the server instead of multiple instances of the same version, you must install them in different locations. If you install binary distributions (without RPM), they will be installed in a directory that contains different version numbers. If you install from source code, the easiest way is to use the-with-prefix option to separate the different distributions during each version of the configure configuration MySQL installation, which will allow everything to be installed in a separate directory, and you can associate the directory domain distribution version number, for example, you can configure a MySQL distribution like this, where version is the MySQL version 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 server-specific options, such as TCP/ IP port numbers and socket pathnames (--with-tcp-port and-- with-unix-socket).
If you want to run multiple instances of the same version of the server, any options that must be based on a server-specific setting will need to be specified at run time.
5.2 Startup process for multiple servers
Starting multiple servers is more complex than using one server. Because both safe_mysqld and mysql.server work best on a single server setup. It is recommended that you take a closer look at safe_mysqld and use it as the basis for your startup process. With your modified version, you can tailor it more accurately 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 different server setting, you can only use this file for the same settings for all servers. If the server has a different compiled data directory location, you can specify all server-specific settings in the my.cnf in each server's data directory, and use DATADIR/my.cnf to specify server-specific settings, where the DATADIR varies from server to server.
Another way to specify server options is to use-- default-file=path_name as the first option on the command line, telling the server to read options from a file called path_name, so that you can put a server option in a file that is unique to the server, and then tell the server to read the file at startup. Note that if you specify this option, the usual option files such as / etc/my.cnf will not be used.
At this point, I believe that everyone on the "linux MySQL server start and stop method" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.