Start and stop of Mysql server (I)

  • 2020-05-06 11:44:48
  • OfStack

Before we discuss how to start the MySQL server, let's consider what user you should run the MySQL server as. The server can be started manually or automatically. If you start it manually, the server starts as the user you logged on Unix (Linux), that is, if you logged on Unix with paul and started the server, it runs with paul; If you switch to root with the su command and run the startup server, it runs as root. However, in most cases you may not want to start the server manually, most likely if you arrange for the MySQL server to be started automatically when the system boots as part of the standard boot process, which is performed by the Unix user root of the system under Unix, and any processes running in this process are run with root privileges.  
You should keep in mind two objectives 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 really need root permissions, which MySQL does not.  

        you want the server to run with the same user, using a user at this time and other times with a different user to run the server is very inconvenient, this creates for files and directories to the data directory is created with different owner, and may cause the server cannot access the database or table, this watch you run to which user. Running the server with the same user is one way you can avoid this problem.    

To run the server for a normal, non-privileged user, follow these steps:  

        selects a user to run the server, mysqld can run with any user. But conceptually it is 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 user names and user group names, respectively.  

If you have MySQL installed under your own account and do not have special administrative rights 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 the rpm file on RedHat    , the installation will automatically create an account named mysql and replace mysqladm with mysql.  

        if necessary, use the system's usual procedure for creating users to create server accounts, you need to use root to do it.    
        if the server is running, stop it.    

        modifies 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  

        modifies the permissions of the data directory and any subdirectories and files so that they are only accessible 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    
        pay attention to symbolic join when you set the data directory and its contents to the master and mode. 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 not where you belong, which is a bit of a hassle, you may need an root identity.  

After you have completed the above procedure, you should ensure that you always start the server when you log in as mysqladm or root. In the latter case, be sure to specify the --user=mysqladm option to enable the server to switch its user ID to mysqladm (also for system startup).  

        -- 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 root.  



Method  
to start the server
After we have identified the account for running the server, you can choose how to start the server. You can run it manually from the command line or automatically during system boot. There are three main methods for starting a server:  

        directly calls mysqld.  

        this is probably the least commonly used method and is not recommended to be used in large amounts, so this article does not cover it in detail.    

        calls the safe_mysqld script.  

        safe_mysqld attempts to locate the server program and data directory. The server is then invoked with an option that reflects 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 of it. After starting the server, safe_mysqld also monitors it and restarts it if it dies. safe_mysqld is often used in BSD style Unix systems.  

If you start sqfe_mysqld with root or during system boot, the error log is owned by root. This may cause an "permission   denied" (permission denied) error if you later try to call safe_mysqld with an unprivileged user. Delete the error log and try again.    

        calls the mysql.server script.  

This script starts the server by intentionally using safe_mysqld.mysql.server on the start and stop system of System   V with several script directories containing scripts that are tapped when the machine enters or exits a given run level. It can use an 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 of the MySQL source code distribution. 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 under the MySQL source distribution. If you want to use them, you need to copy them to the appropriate directory.  

        on BSD style systems (FreeBSD, OpenBSD, etc.), there are usually several files in the /etc directory that initialize the service at boot time. These files usually have names beginning with "rc", and it may be a file named "rc. On such a system, you might add lines like the following to the rc.local file to start the server (if the directory of safe_mysqld is different on your system, just change it) :  

        if   [   -x   /usr/local/bin/safe_mysqld   ];   then  
        /usr/local/bin/safe_mysqld   &  
        fi  
        for System   V style systems, you can install mysql.server by placing it in the appropriate boot directory under /etc. If you run Linux and install MySQL from an RPM file, this is done for you, otherwise install the script in the main boot directory and place the connection to it in the appropriate runlevel directory. You can also enable scripts to be started 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, in Linux   PPC, directory is/etc rc d and/etc rc d/rc3 d, so that you can install 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, main script directory is/etc init d, and run level directory is/etc rd2 d, so the command would look 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 an start parameter. If you have the chkconfig command (on Linux), you can use it to help install the mysql.server script instead of running the above command manually.  

        specifies the startup option  

If you want to specify additional boot options when the server starts, you can do this in two ways. You can modify the startup script you use (safe_mysqld or mysql.server) and specify options directly on the line of the calling server, or in an options file. It is recommended that you specify options in a global options file if possible, which is usually located at /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 cannot correctly select the local time zone and returns the time value as 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. Find the line that started the server and add the following command before that line:  

        TZ=US/Central  
        export   TZ  

The syntax of the command above         is Solaris, the syntax may be different for other systems, please refer to the relevant manual. If you do change your startup script, remember 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 old and new versions of the script to see what changes you need to rebuild.  

        check your table at startup  

In addition to scheduling your server to start at boot time, you may want to install the myisamchk and isamchk scripts to check your tables before the server starts. You may restart after a crash, and it is possible that the table has been compromised. Checking it before starting is a good way to find problems.

Related articles: