CentOS 7 安装 MySQL 5.6.4 -- 通过二进制包方式安装

一、查看系统已经安装的mysql数据库
1.查看系统是否已经安装了 mariadb

[vicky@localhost ~]$ rpm -qa |grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64

2.查看是否已经存在配置文件 my.cnf

[vicky@localhost ~]$ cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

3.查看是否已经存在 mysql 用户和mysql 组。
说明: 我新装的 CentOS 7 是没有的。

[vicky@localhost ~]$ cat /etc/group |grep mysql
[vicky@localhost ~]$ cat /etc/passwd |grep mysql

4.查看系统现在有哪些 mysql 的信息

[vicky@localhost ~]$ whereis mysql
mysql: /usr/lib64/mysql /usr/share/mysql
[vicky@localhost ~]$ cd /usr/lib64/mysql/
[vicky@localhost mysql]$ ll
总用量 3064
lrwxrwxrwx. 1 root root      24 11月  7 23:03 libmysqlclient.so.18 -> libmysqlclient.so.18.0.0
-rwxr-xr-x. 1 root root 3135736 11月 15 2016 libmysqlclient.so.18.0.0
drwxr-xr-x. 2 root root      54 11月  7 23:03 plugin
[vicky@localhost mysql]$ cd plugin/
[vicky@localhost plugin]$ ll
总用量 20
-rwxr-xr-x. 1 root root 11288 11月 15 2016 dialog.so
-rwxr-xr-x. 1 root root  7184 11月 15 2016 mysql_clear_password.so
[vicky@localhost plugin]$ cd /usr/share/mysql
[vicky@localhost mysql]$ ll
总用量 4
drwxr-xr-x. 2 root root 4096 11月  7 23:03 charsets
drwxr-xr-x. 2 root root   24 11月  7 23:03 czech
drwxr-xr-x. 2 root root   24 11月  7 23:03 danish
drwxr-xr-x. 2 root root   24 11月  7 23:03 dutch
drwxr-xr-x. 2 root root   24 11月  7 23:03 english
drwxr-xr-x. 2 root root   24 11月  7 23:03 estonian
drwxr-xr-x. 2 root root   24 11月  7 23:03 french
drwxr-xr-x. 2 root root   24 11月  7 23:03 german
drwxr-xr-x. 2 root root   24 11月  7 23:03 greek
drwxr-xr-x. 2 root root   24 11月  7 23:03 hungarian
drwxr-xr-x. 2 root root   24 11月  7 23:03 italian
drwxr-xr-x. 2 root root   24 11月  7 23:03 japanese
drwxr-xr-x. 2 root root   24 11月  7 23:03 korean
drwxr-xr-x. 2 root root   24 11月  7 23:03 norwegian
drwxr-xr-x. 2 root root   24 11月  7 23:03 norwegian-ny
drwxr-xr-x. 2 root root   24 11月  7 23:03 polish
drwxr-xr-x. 2 root root   24 11月  7 23:03 portuguese
drwxr-xr-x. 2 root root   24 11月  7 23:03 romanian
drwxr-xr-x. 2 root root   24 11月  7 23:03 russian
drwxr-xr-x. 2 root root   24 11月  7 23:03 serbian
drwxr-xr-x. 2 root root   24 11月  7 23:03 slovak
drwxr-xr-x. 2 root root   24 11月  7 23:03 spanish
drwxr-xr-x. 2 root root   24 11月  7 23:03 swedish
drwxr-xr-x. 2 root root   24 11月  7 23:03 ukrainian
[vicky@localhost mysql]$ 

二、卸载:
命令:

[root@localhost mysql]# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64

卸载之后,查看各个目录的文件:

[root@localhost mysql]# rpm -qa | grep mariadb
[root@localhost mysql]# cat /etc/my.cnf
cat: /etc/my.cnf: 没有那个文件或目录

[root@localhost mysql]# whereis mysql
mysql: /usr/lib64/mysql

[root@localhost mysql]# cd /usr/lib64/mysql/
[root@localhost mysql]# ll
总用量 0
drwxr-xr-x. 2 root root 6 11月  8 00:12 plugin
[root@localhost mysql]# cd plugin/
[root@localhost plugin]# ll
总用量 0

三、安装:
1.从 mysql 的官网下载所需要的 mysql 版本的安装文件。
官网下载地址:https://dev.mysql.com/downloads/mysql/

2.解压到自己需要的文件夹下
我直接解压到用户根目录下了。

[root@localhost vicky]# tar -zvxf mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz

3.将解压后的文件夹拷贝到 /usr/local/mysql 目录下。
说明:你也可以指定其他自定义的目录,但是,后续需要修改设置。本篇就只将放在该目录下的安装了。

[root@localhost vicky]# cp mysql-5.6.42-linux-glibc2.12-x86_64 /usr/local/mysql -r

4.创建用户和组

[root@localhost vicky]# groupadd mysql    
[root@localhost vicky]# cat /etc/group |grep mysql
mysql:x:1001:
[root@localhost vicky]# useradd -r -g mysql mysql 
[root@localhost vicky]# cat /etc/passwd |grep mysql
mysql:x:988:1001::/home/mysql:/bin/bash

5.将 /usr/local/mysql/support-files/my-default.cnf 拷贝到 /etc 下并修改文件名为 my.cnf

[root@localhost support-files]# cp my-default.cnf /etc/my.cnf

6.修改文件 /etc/my.cnf
指定数据路径: datadir = /usr/local/mysql/data
以及用户: user=mysql

    [root@localhost support-files]# vi /etc/my.cnf 
    [vicky@localhost ~]$ cat /etc/my.cnf
    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
    # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
    # *** default location during install, and will be replaced if you
    # *** upgrade to a newer version of MySQL.

    [mysqld]

    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M

    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin

    # These are commonly set, remove the # and set as required.
    # basedir = .....
    datadir =  /usr/local/mysql/data
    # port = .....
    # server_id = .....
    # socket = .....
    user=mysql

    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M 

    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

7.安装 mysql: ./scripts/mysql_install_db --user=mysql

    [root@localhost mysql]# ./scripts/mysql_install_db --user=mysql
    Installing MySQL system tables...2018-11-08 00:26:40 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2018-11-08 00:26:40 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
    2018-11-08 00:26:40 0 [Note] ./bin/mysqld (mysqld 5.6.42) starting as process 13578 ...
    2018-11-08 00:26:40 13578 [Note] InnoDB: Using atomics to ref count buffer pool pages
    2018-11-08 00:26:40 13578 [Note] InnoDB: The InnoDB memory heap is disabled
    2018-11-08 00:26:40 13578 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
    2018-11-08 00:26:40 13578 [Note] InnoDB: Memory barrier is not used
    2018-11-08 00:26:40 13578 [Note] InnoDB: Compressed tables use zlib 1.2.11
    2018-11-08 00:26:40 13578 [Note] InnoDB: Using Linux native AIO
    2018-11-08 00:26:40 13578 [Note] InnoDB: Using CPU crc32 instructions
    2018-11-08 00:26:40 13578 [Note] InnoDB: Initializing buffer pool, size = 128.0M
    2018-11-08 00:26:40 13578 [Note] InnoDB: Completed initialization of buffer pool
    2018-11-08 00:26:40 13578 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
    2018-11-08 00:26:40 13578 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
    2018-11-08 00:26:40 13578 [Note] InnoDB: Database physically writes the file full: wait...
    2018-11-08 00:26:40 13578 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
    2018-11-08 00:26:40 13578 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
    2018-11-08 00:26:40 13578 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
    2018-11-08 00:26:40 13578 [Warning] InnoDB: New log files created, LSN=45781
    2018-11-08 00:26:40 13578 [Note] InnoDB: Doublewrite buffer not found: creating new
    2018-11-08 00:26:40 13578 [Note] InnoDB: Doublewrite buffer created
    2018-11-08 00:26:40 13578 [Note] InnoDB: 128 rollback segment(s) are active.
    2018-11-08 00:26:40 13578 [Warning] InnoDB: Creating foreign key constraint system tables.
    2018-11-08 00:26:40 13578 [Note] InnoDB: Foreign key constraint system tables created
    2018-11-08 00:26:40 13578 [Note] InnoDB: Creating tablespace and datafile system tables.
    2018-11-08 00:26:40 13578 [Note] InnoDB: Tablespace and datafile system tables created.
    2018-11-08 00:26:40 13578 [Note] InnoDB: Waiting for purge to start
    2018-11-08 00:26:40 13578 [Note] InnoDB: 5.6.42 started; log sequence number 0
    2018-11-08 00:26:42 13578 [Note] Binlog end
    2018-11-08 00:26:42 13578 [Note] InnoDB: FTS optimize thread exiting.
    2018-11-08 00:26:42 13578 [Note] InnoDB: Starting shutdown...
    2018-11-08 00:26:43 13578 [Note] InnoDB: Shutdown completed; log sequence number 1625977
    OK

    Filling help tables...2018-11-08 00:26:43 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2018-11-08 00:26:43 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
    2018-11-08 00:26:43 0 [Note] ./bin/mysqld (mysqld 5.6.42) starting as process 13600 ...
    2018-11-08 00:26:43 13600 [Note] InnoDB: Using atomics to ref count buffer pool pages
    2018-11-08 00:26:43 13600 [Note] InnoDB: The InnoDB memory heap is disabled
    2018-11-08 00:26:43 13600 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
    2018-11-08 00:26:43 13600 [Note] InnoDB: Memory barrier is not used
    2018-11-08 00:26:43 13600 [Note] InnoDB: Compressed tables use zlib 1.2.11
    2018-11-08 00:26:43 13600 [Note] InnoDB: Using Linux native AIO
    2018-11-08 00:26:43 13600 [Note] InnoDB: Using CPU crc32 instructions
    2018-11-08 00:26:43 13600 [Note] InnoDB: Initializing buffer pool, size = 128.0M
    2018-11-08 00:26:43 13600 [Note] InnoDB: Completed initialization of buffer pool
    2018-11-08 00:26:43 13600 [Note] InnoDB: Highest supported file format is Barracuda.
    2018-11-08 00:26:43 13600 [Note] InnoDB: 128 rollback segment(s) are active.
    2018-11-08 00:26:43 13600 [Note] InnoDB: Waiting for purge to start
    2018-11-08 00:26:43 13600 [Note] InnoDB: 5.6.42 started; log sequence number 1625977
    2018-11-08 00:26:43 13600 [Note] Binlog end
    2018-11-08 00:26:43 13600 [Note] InnoDB: FTS optimize thread exiting.
    2018-11-08 00:26:43 13600 [Note] InnoDB: Starting shutdown...
    2018-11-08 00:26:45 13600 [Note] InnoDB: Shutdown completed; log sequence number 1625987
    OK

    To start mysqld at boot time you have to copy
    support-files/mysql.server to the right place for your system

    PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
    To do so, start the server, then issue the following commands:

      ./bin/mysqladmin -u root password 'new-password'
      ./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

    Alternatively you can run:

      ./bin/mysql_secure_installation

    which will also give you the option of removing the test
    databases and anonymous user created by default.  This is
    strongly recommended for production servers.

    See the manual for more instructions.

    You can start the MySQL daemon with:

      cd . ; ./bin/mysqld_safe &

    You can test the MySQL daemon with mysql-test-run.pl

      cd mysql-test ; perl mysql-test-run.pl

    Please report any problems at http://bugs.mysql.com/

    The latest information about MySQL is available on the web at

      http://www.mysql.com

    Support MySQL by buying support/licenses at http://shop.mysql.com

    New default config file was created as ./my.cnf and
    will be used by default by the server when you start it.
    You may edit this file to change server settings

    WARNING: Default config file /etc/my.cnf exists on the system
    This file will be read by default by the MySQL server
    If you do not want to use this, either remove it, or use the
    --defaults-file argument to mysqld_safe when starting the server

说明:可以先安装,后将 /usr/local/mysql/support-files/my-default.cnf 拷贝到 /etc 。否则就会向本例中的安装末尾出现如下警告.

   WARNING: Default config file /etc/my.cnf exists on the system
    This file will be read by default by the MySQL server
    If you do not want to use this, either remove it, or use the
    --defaults-file argument to mysqld_safe when starting the server

8.配置.启动 mysqld。 命令:[root@localhost mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

        [root@localhost mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
        2018-11-08 00:29:17 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
        2018-11-08 00:29:17 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
        2018-11-08 00:29:17 0 [Note] ./bin/mysqld (mysqld 5.6.42) starting as process 13654 ...
        2018-11-08 00:29:18 13654 [Note] Plugin 'FEDERATED' is disabled.
        2018-11-08 00:29:18 13654 [Note] InnoDB: Using atomics to ref count buffer pool pages
        2018-11-08 00:29:18 13654 [Note] InnoDB: The InnoDB memory heap is disabled
        2018-11-08 00:29:18 13654 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
        2018-11-08 00:29:18 13654 [Note] InnoDB: Memory barrier is not used
        2018-11-08 00:29:18 13654 [Note] InnoDB: Compressed tables use zlib 1.2.11
        2018-11-08 00:29:18 13654 [Note] InnoDB: Using Linux native AIO
        2018-11-08 00:29:18 13654 [Note] InnoDB: Using CPU crc32 instructions
        2018-11-08 00:29:18 13654 [Note] InnoDB: Initializing buffer pool, size = 128.0M
        2018-11-08 00:29:18 13654 [Note] InnoDB: Completed initialization of buffer pool
        2018-11-08 00:29:19 13654 [Note] InnoDB: Highest supported file format is Barracuda.
        2018-11-08 00:29:20 13654 [Note] InnoDB: 128 rollback segment(s) are active.
        2018-11-08 00:29:20 13654 [Note] InnoDB: Waiting for purge to start
        2018-11-08 00:29:20 13654 [Note] InnoDB: 5.6.42 started; log sequence number 1625987
        2018-11-08 00:29:21 13654 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 47bc294d-e2aa-11e8-a391-52540029415a.
        2018-11-08 00:29:21 13654 [Note] Server hostname (bind-address): '*'; port: 3306
        2018-11-08 00:29:21 13654 [Note] IPv6 is available.
        2018-11-08 00:29:21 13654 [Note]   - '::' resolves to '::';
        2018-11-08 00:29:21 13654 [Note] Server socket created on IP: '::'.
        2018-11-08 00:29:22 13654 [Note] Event Scheduler: Loaded 0 events
        2018-11-08 00:29:22 13654 [Note] ./bin/mysqld: ready for connections.
        Version: '5.6.42'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)

此时,终端会一直停留在这,查看进程,发现 mysql 已经启动了。

9.验证
另开一个终端,查看进程信息

[root@localhost vicky]# ps -ef|grep mysql
mysql     13654  13305  0 00:29 pts/0    00:00:00 ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
root      13800  13763  0 00:31 pts/1    00:00:00 grep --color=auto mysql

10.添加环境变量。
此时直接通过 mysql 命令启动 mysql 的命令行,将会提示 找不到 mysql 命令,查看环境变量会发现没有配置 mysql 的路径。
注:查看环境变量的方式。 echo $PATH

  1. 打开 /etc/profile, 在文件末尾添加如下两行信息:
    PATH=$PATH:/usr/local/mysql/bin
    export PATH

  2. 使用命令 source /etc/profile 使修改立即生效。

    [root@localhost vicky]# mysql -uroot
    bash: mysql: 未找到命令...
    [root@localhost vicky]# vi /etc/profile
    [root@localhost vicky]# source /etc/profile
    

    11.启动 mysql 命令行,并修改 root 用户密码。
    说明:
    新安装的 mysql 默认的 root 用户的密码为空,所以在连接的输入密码的时候,什么都不输入直接回车即可。
    登录之后通过语句修改 root 用户的密码,注意是:password=PASSWORD(‘root’)
    update mysql.user set password=PASSWORD(‘root’) where user=‘root’;
    然后,输入: fush privileges;
    使用 quit 退出。
    重新使用 root 用户和root 密码登录即可。

            [root@localhost init.d]# mysql -uroot -p
            Enter password: 
            Welcome to the MySQL monitor.  Commands end with ; or \g.
            Your MySQL connection id is 1
            Server version: 5.6.42 MySQL Community Server (GPL)
    
            Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
            Oracle is a registered trademark of Oracle Corporation and/or its
            affiliates. Other names may be trademarks of their respective
            owners.
    
            Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
            mysql> show databases; #查看现有数据库
            +--------------------+
            | Database           |
            +--------------------+
            | information_schema |
            | mysql              |
            | performance_schema |
            | test               |
            +--------------------+
            4 rows in set (0.03 sec)
    
            mysql> select host,user,password from mysql.user where user = 'root'; #查看 root 用户信息
            +-----------------------+------+----------+
            | host                  | user | password |
            +-----------------------+------+----------+
            | localhost             | root |          |
            | localhost.localdomain | root |          |
            | 127.0.0.1             | root |          |
            | ::1                   | root |          |
            +-----------------------+------+----------+
            4 rows in set (0.01 sec)
    
            mysql> update mysql.user set password=PASSWORD('root') where user='root';   # 更改 root 用户密码
            Query OK, 4 rows affected (0.02 sec)
            Rows matched: 4  Changed: 4  Warnings: 0
    
            mysql> fush privileges;         # 刷新系统权限的相关表
            ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fush privileges' at line 1
            mysql> flush privileges;
            Query OK, 0 rows affected (0.04 sec)
    
            mysql> quit;                    # 退出
            Bye
    
            [root@localhost init.d]# mysql -uroot -p
            Enter password:                                             # 重新登录,这里输入新修改的密码 root
            Welcome to the MySQL monitor.  Commands end with ; or \g.
            Your MySQL connection id is 2
            Server version: 5.6.42 MySQL Community Server (GPL)
    
            Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
            Oracle is a registered trademark of Oracle Corporation and/or its
            affiliates. Other names may be trademarks of their respective
            owners.
    
            Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
            mysql> select host,user,password from mysql.user where user='root';     # 可以查看现在 root 用户的信息,密码已经有了。
            +-----------------------+------+-------------------------------------------+
            | host                  | user | password                                  |
            +-----------------------+------+-------------------------------------------+
            | localhost             | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
            | localhost.localdomain | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
            | 127.0.0.1             | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
            | ::1                   | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
            +-----------------------+------+-------------------------------------------+
            4 rows in set (0.00 sec)
    
            mysql> [root@l
    

四、设置通过 service 启停服务
1.此时无法通过 service 命令进行起停,查看 mysqld 服务的运行情况。

[root@localhost init.d]# service mysqld status
Redirecting to /bin/systemctl status  mysqld.service
Unit mysqld.service could not be found.

2.如果 mysql 已经启动,kill 掉已经启动的进程:

[root@localhost ~]# ps -ef |grep mysql
mysql     13654  13305  0 00:29 ?        00:00:01 ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
root      14079  14036  0 00:37 pts/1    00:00:00 grep --color=auto mysql
[root@localhost ~]# kill 13654
[root@localhost ~]# ps -ef |grep mysql
root      14100  14036  0 00:37 pts/1    00:00:00 grep --color=auto mysql

3.将 /usr/local/mysql/support-files/mysql.server 拷贝到 /etc/rc.d/init.d/ 资源目录下并改名为 mysqld。注意查看其权限,必须有可执行权限才行。如果没有可以通过下面的命令修改。
修改权限命令: chmod +x /etc/rc.d/init.d/mysqld

[root@localhost support-files]# cd /etc/rc.d/init.d/
[root@localhost init.d]# ll
总用量 32
-rw-r--r--. 1 root root 15131 9月  12 2016 functions
-rwxr-xr-x. 1 root root  2989 9月  12 2016 netconsole
-rwxr-xr-x. 1 root root  6643 9月  12 2016 network
-rw-r--r--. 1 root root  1160 11月  7 2016 README
[root@localhost init.d]# cp /usr/local/mysql/support-files/mysql.server ./mysqld
[root@localhost init.d]# ll
总用量 44
-rw-r--r--. 1 root root 15131 9月  12 2016 functions
-rwxr-xr-x. 1 root root 10565 11月  8 00:40 mysqld
-rwxr-xr-x. 1 root root  2989 9月  12 2016 netconsole
-rwxr-xr-x. 1 root root  6643 9月  12 2016 network
-rw-r--r--. 1 root root  1160 11月  7 2016 README

4.启动 mysqld 服务,启动成功。

[root@localhost init.d]# service mysqld start
Unit mysqld.service could not be found.
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
..... SUCCESS! 

5.查看 mysqld 的状态

[root@localhost init.d]# service mysqld status
SUCCESS! MySQL running (14393)

6.查看 mysql 进程信息。

[root@localhost init.d]# ps -ef|grep mysql
root      14258      1  0 00:40 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/localhost.localdomain.pid
mysql     14393  14258  1 00:40 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/usr/local/mysql/data/localhost.localdomain.pid
root      14452  14036  0 00:41 pts/1    00:00:00 grep --color=auto mysql

五、设置系统开机自启
通过以上步骤安装好 mysql 之后,关机再次开机时,mysqld 服务会停掉。

[vicky@localhost ~]$ service mysqld status
 ERROR! MySQL is not running

执行下面的命令将mysqld服务加入到系统服务: chkconfig --add mysqld 试下 chkconfig mysqld on
执行下面的命令检查mysqld服务是否已经生效: chkconfig --list mysqld
命令输出类似下面的结果:
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
表明mysqld服务已经生效,在2、3、4、5运行级别随系统启动而自动启动,以后可以使用service命令控制mysql的启动和停止。

[root@localhost init.d]# chkconfig --add mysqld
[root@localhost init.d]# chkconfig --list mysqld

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关

执行下面的命令可以改变开机自启动的运行级别为3、5: chkconfig --level 35 mysqld on

也可使用启动: /etc/init.d/mysqld start

六.开机重启,查看 mysqld 运行情况。发现报错。

[vicky@localhost ~]$ su
密码:
[root@localhost vicky]# service mysqld status
 SUCCESS! MySQL running (2116)

七、强化安全,删除匿名用户。对于个人学习而言,此步不是必须的,但是对于放到生产环境的需要执行。
1.可以在 root 用户下执行: ./bin/mysql_secure_installation 按照提示删除匿名账户和空密码的账户。
2.手动删除的方法是使用 sql 语句。
mysql -uroot -p
delete from mysql.user where user=’’;
flush privileges;

运行示例:

[root@localhost bin]# ./mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!




All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


Cleaning up...
[root@localhost bin]# su vicky
[vicky@localhost bin]$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from user where user='';
Empty set (0.00 sec)

mysql> 

/usr/local/mysql 目录下的磁盘空间可能不够,可以将 data 目录移动到 /home/mysql/data 下,并建立软连接。

[root@localhost mysql]# ll
总用量 52
drwxr-xr-x.  2 root  root   4096 11月  8 09:36 bin
-rw-r--r--.  1 root  root  17987 11月  8 00:16 COPYING
drwxr-xr-x.  4 mysql mysql   180 11月  8 09:36 data
drwxr-xr-x.  2 root  root     55 11月  8 00:16 docs
drwxr-xr-x.  3 root  root   4096 11月  8 00:16 include
drwxr-xr-x.  3 root  root   4096 11月  8 00:16 lib
drwxr-xr-x.  4 root  root     30 11月  8 00:16 man
-rw-r--r--.  1 root  root    943 11月  8 00:26 my.cnf
drwxr-xr-x. 10 root  root   4096 11月  8 00:16 mysql-test
-rw-r--r--.  1 root  root   2496 11月  8 00:16 README
drwxr-xr-x.  2 root  root     30 11月  8 00:16 scripts
drwxr-xr-x. 28 root  root   4096 11月  8 00:16 share
drwxr-xr-x.  4 root  root   4096 11月  8 00:16 sql-bench
drwxr-xr-x.  2 root  root    136 11月  8 00:16 support-files
[root@localhost mysql]# mv data /home/mysql
[root@localhost mysql]# ll
总用量 52
drwxr-xr-x.  2 root root  4096 11月  8 09:36 bin
-rw-r--r--.  1 root root 17987 11月  8 00:16 COPYING
drwxr-xr-x.  2 root root    55 11月  8 00:16 docs
drwxr-xr-x.  3 root root  4096 11月  8 00:16 include
drwxr-xr-x.  3 root root  4096 11月  8 00:16 lib
drwxr-xr-x.  4 root root    30 11月  8 00:16 man
-rw-r--r--.  1 root root   943 11月  8 00:26 my.cnf
drwxr-xr-x. 10 root root  4096 11月  8 00:16 mysql-test
-rw-r--r--.  1 root root  2496 11月  8 00:16 README
drwxr-xr-x.  2 root root    30 11月  8 00:16 scripts
drwxr-xr-x. 28 root root  4096 11月  8 00:16 share
drwxr-xr-x.  4 root root  4096 11月  8 00:16 sql-bench
drwxr-xr-x.  2 root root   136 11月  8 00:16 support-files
[root@localhost mysql]# ln -s /home/mysql/data  .
[root@localhost mysql]# ll
总用量 52
drwxr-xr-x.  2 root root  4096 11月  8 09:36 bin
-rw-r--r--.  1 root root 17987 11月  8 00:16 COPYING
lrwxrwxrwx   1 root root    16 11月  8 09:40 data -> /home/mysql/data
drwxr-xr-x.  2 root root    55 11月  8 00:16 docs
drwxr-xr-x.  3 root root  4096 11月  8 00:16 include
drwxr-xr-x.  3 root root  4096 11月  8 00:16 lib
drwxr-xr-x.  4 root root    30 11月  8 00:16 man
-rw-r--r--.  1 root root   943 11月  8 00:26 my.cnf
drwxr-xr-x. 10 root root  4096 11月  8 00:16 mysql-test
-rw-r--r--.  1 root root  2496 11月  8 00:16 README
drwxr-xr-x.  2 root root    30 11月  8 00:16 scripts
drwxr-xr-x. 28 root root  4096 11月  8 00:16 share
drwxr-xr-x.  4 root root  4096 11月  8 00:16 sql-bench
drwxr-xr-x.  2 root root   136 11月  8 00:16 support-files

[root@localhost mysql]# cd /home/mysql
[root@localhost mysql]# ll
总用量 110632
-rw-rw----. 1 mysql mysql       56 11月  8 00:29 auto.cnf
-rw-rw----. 1 mysql mysql 12582912 11月  8 02:10 ibdata1
-rw-rw----. 1 mysql mysql 50331648 11月  8 02:10 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 11月  8 00:26 ib_logfile1
-rw-rw----. 1 mysql mysql    23759 11月  8 02:10 localhost.localdomain.err
-rw-rw----  1 mysql mysql        5 11月  8 02:10 localhost.localdomain.pid
drwx------. 2 mysql mysql     4096 11月  8 00:26 mysql
drwx------. 2 mysql mysql     4096 11月  8 00:26 performance_schema
[root@localhost mysql]# ll /usr/local/mysql/data
lrwxrwxrwx 1 root root 16 11月  8 09:40 /usr/local/mysql/data -> /home/mysql/data

感想:
花费两天的时间,经过了无数次的安装卸载,最终在 linux 下 成功的安装了 MySQL ,期间也因为对 Linux 系统安装软件的方法,以及系统各个目录的作用,和 MySQL 各个脚本的作用不了解,只是按照网上教程安装,但是每个人安装时系统环境不同,完全按照别人的指导,可能导致安装过程会遇到各种问题。
虽然会花费时间,但是,解决问题的过程也是学习的过程,只有经历了安装 --》遇到问题–》解决问题–》重新安装–》遇到问题–》解决问题…这样一个循环过程,才能真正掌握,理解。
对个人而言,涉及的需要复习的知识点:
1. Linux 下 通过 chkconfig 命令配置系统服务的方法;
2. Linux 的运行级别
3. 软连接的使用
4. 添加用户和组的方法
5. 文件和目录的权限问题
有待学习的地方:
1. MySQL 的使用
2. Linux 操作系统的学习

说明:
本博客的内容仅是个人学习中遇到的问题,以及解决方法的记录。如有错误,欢迎指正,如有侵权请告知修改。

猜你喜欢

转载自blog.csdn.net/lijing_2011/article/details/83856911