1. 程式人生 > >CentOS 7 安裝 MySQL 5.6.4 -- 通過二進位制包方式安裝

CentOS 7 安裝 MySQL 5.6.4 -- 通過二進位制包方式安裝

一、檢視系統已經安裝的mysql資料庫
1.檢視系統是否已經安裝了 mariadb

[[email protected] ~]$ rpm -qa |grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64

2.檢視是否已經存在配置檔案 my.cnf

[[email protected] ~]$ 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 是沒有的。

[[email protected] ~]$ cat /etc/group |grep mysql
[[email protected] ~]$ cat /etc/passwd |grep mysql

4.檢視系統現在有哪些 mysql 的資訊

[[email protected] ~]$ whereis mysql
mysql: /usr/lib64/mysql /usr/share/mysql
[[email protected] ~]$ cd /usr/lib64/mysql/
[
[email protected]
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 [[email protected] mysql]$ cd plugin/ [[email protected]
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 [[email protected] plugin]$ cd /usr/share/mysql [[email protected] 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 [[email protected] mysql]$

二、解除安裝:
命令:

[[email protected] mysql]# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64

解除安裝之後,檢視各個目錄的檔案:

[[email protected] mysql]# rpm -qa | grep mariadb
[[email protected] mysql]# cat /etc/my.cnf
cat: /etc/my.cnf: 沒有那個檔案或目錄

[[email protected] mysql]# whereis mysql
mysql: /usr/lib64/mysql

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

三、安裝:
1.從 mysql 的官網下載所需要的 mysql 版本的安裝檔案。
官網下載地址:https://dev.mysql.com/downloads/mysql/

2.解壓到自己需要的資料夾下
我直接解壓到使用者根目錄下了。

[[email protected] vicky]# tar -zvxf mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz

3.將解壓後的資料夾拷貝到 /usr/local/mysql 目錄下。
說明:你也可以指定其他自定義的目錄,但是,後續需要修改設定。本篇就只將放在該目錄下的安裝了。

[[email protected] vicky]# cp mysql-5.6.42-linux-glibc2.12-x86_64 /usr/local/mysql -r

4.建立使用者和組

[[email protected] vicky]# groupadd mysql    
[[email protected] vicky]# cat /etc/group |grep mysql
mysql:x:1001:
[[email protected] vicky]# useradd -r -g mysql mysql 
[[email protected] 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

[[email protected] support-files]# cp my-default.cnf /etc/my.cnf

6.修改檔案 /etc/my.cnf
指定資料路徑: datadir = /usr/local/mysql/data
以及使用者: user=mysql

    [[email protected] support-files]# vi /etc/my.cnf 
    [[email protected] ~]$ 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

    [[email protected] 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。 命令:[[email protected] mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

        [[email protected] 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.驗證
另開一個終端,檢視程序資訊

[[email protected] 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 使修改立即生效。

    [[email protected] vicky]# mysql -uroot
    bash: mysql: 未找到命令...
    [[email protected] vicky]# vi /etc/profile
    [[email protected] 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 密碼登入即可。

            [[email protected] 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
    
            [[email protected] 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> [[email protected]
    

四、設定通過 service 啟停服務
1.此時無法通過 service 命令進行起停,檢視 mysqld 服務的執行情況。

[[email protected] init.d]# service mysqld status
Redirecting to /bin/systemctl status  mysqld.service
Unit mysqld.service could not be found.

2.如果 mysql 已經啟動,kill 掉已經啟動的程序:

[[email protected] ~]# 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
[[email protected] ~]# kill 13654
[[email protected] ~]# 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

[[email protected] support-files]# cd /etc/rc.d/init.d/
[[email protected] 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
[[email protected] init.d]# cp /usr/local/mysql/support-files/mysql.server ./mysqld
[[email protected] 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 服務,啟動成功。

[[email protected] 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 的狀態

[[email protected] init.d]# service mysqld status
SUCCESS! MySQL running (14393)

6.檢視 mysql 程序資訊。

[[email protected] 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 服務會停掉。

[[email protected] ~]$ 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的啟動和停止。

[[email protected] init.d]# chkconfig --add mysqld
[[email protected] 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 執行情況。發現報錯。

[[email protected] ~]$ su
密碼:
[[email protected] 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;

執行示例:

[[email protected] 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...
[[email protected] bin]# su vicky
[[email protected] 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 下,並建立軟連線。

[[email protected] 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
[[email protected] mysql]# mv data /home/mysql
[[email protected] 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
[[email protected] mysql]# ln -s /home/mysql/data  .
[[email protected] 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

[[email protected] mysql]# cd /home/mysql
[[email protected] 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
[[email protected] 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 作業系統的學習

說明:
本部落格的內容僅是個人學習中遇到的問題,以及解決方法的記錄。如有錯誤,歡迎指正,如有侵權請告知修改。