Centos 7.4 通过 tar 包方式安装 MySQL 8.0

MySQL 8.0 也推出一段时间了,整理一篇安装教程

  1. CentOS7.4系统自带mariadb

     [root@iZ286t0wuf9Z etc]# rpm -qa|grep mariadb
     mariadb-libs-5.5.56-2.el7.x86_64
     [root@iZ286t0wuf9Z etc]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
    

    部分博客需要移除 /etc/my.cnf,经实践,本要执行卸载之前/etc目录下有my.cnf和my.cnf.d,执行卸载之后已经一起移除了。或者可以执行下面代码:

     [root@iZ286t0wuf9Z etc]# rm my.cnf
    

    或文件不存在,会提示

     [root@iZ286t0wuf9Z etc]# rm my.cnf
     rm: cannot remove ‘my.cnf’: No such file or director
    
  2. 检查mysql是否存在

     [root@iZ286t0wuf9Z etc]# rpm -qa | grep mysql
     [root@iZ286t0wuf9Z etc]# 
    

    没有任何输出表示未安装

  3. 创建mysql组和用户

    (1) 先检查是否存在

     [root@iZ286t0wuf9Z etc]# cat /etc/group | grep mysql
     [root@iZ286t0wuf9Z etc]# cat /etc/passwd | grep mysql
     [root@iZ286t0wuf9Z etc]# 
    

    (2) 没有输出,则创建组和用户

     [root@iZ286t0wuf9Z etc]# groupadd mysql
     [root@iZ286t0wuf9Z etc]# useradd -g mysql mysql
    
     [root@iZ286t0wuf9Z etc]# passwd mysql
     Changing password for user mysql.
     New password: 
     Retype new password: 
     passwd: all authentication tokens updated successfully.
     [root@iZ286t0wuf9Z etc]# 
    
  4. 准备安装文件

    截图比较多,附在文章最后,点击跳转

  5. 解压安装

    (1) 把mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz文件移动到/usr/local/mysql目录下,假设文件在/home/ftp目录下

     [root@iZ286t0wuf9Z etc]# mv /home/ftp /usr/local/mysql
    

    (3) 解压

     [root@iZ286t0wuf9Z etc]# cd /usr/local
     [root@iZ286t0wuf9Z local]# tar -xvJf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz 
     mysql-8.0.13-linux-glibc2.12-x86_64/bin/myisam_ftdump
     mysql-8.0.13-linux-glibc2.12-x86_64/bin/myisamchk
     ----------
     #此处省略二百多行 mysql-8.0.13-linux-glibc2.12-x86_64...
     ----------
     mysql-8.0.13-linux-glibc2.12-x86_64/lib/libmysqlharness.so.1
     mysql-8.0.13-linux-glibc2.12-x86_64/lib/libmysqlrouter.so
     mysql-8.0.13-linux-glibc2.12-x86_64/lib/libmysqlrouter.so.1
     [root@iZ286t0wuf9Z local]# mv mysql-8.0.13-linux-glibc2.12-x86_64 mysql
    
  6. 更改所属的组和用户

    注意每次执行的目录

     [root@iZ286t0wuf9Z mysql]# mkdir data
     [root@iZ286t0wuf9Z mysql]# cd ..
     [root@iZ286t0wuf9Z local]# chown -R mysql mysql
     [root@iZ286t0wuf9Z local]# chgrp -R mysql mysql
    
  7. 准备/etc/my.cnf文件

     [root@iZ286t0wuf9Z mysql]# vim /etc/my.cnf
     [mysql]
     # 设置mysql客户端默认字符集
     default-character-set=utf8mb4 
    
     [mysqld]
     # 设置3306端口
     port = 3306 
     # 设置mysql的安装目录
     basedir=/usr/local/mysql
     # 设置mysql数据库的数据的存放目录
     datadir=/usr/local/mysql/data
     # 允许最大连接数
     max_connections=200
     # 服务端默认编码(数据库级别)
     character-set-server=utf8mb4
     # 创建新表时将使用的默认存储引擎
     default-storage-engine=INNODB 
     lower_case_table_names=1
     max_allowed_packet=16M
    
     [root@iZ286t0wuf9Z mysql]# chown 777 /etc/my.cnf
    
  8. 执行初始化

     [root@iZ286t0wuf9Z mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
     -bash: bin/mysql_install_db: No such file or directory
    

    切换到/usr/local/mysql/bin目录下,发现确实没有 mysql_install_db,改成 bin/mysqld --initialize

     [root@iZ286t0wuf9Z mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
     bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
    

    又报错,执行下面命令

     [root@iZ286t0wuf9Z mysql]#  yum install -y libaio
     Loaded plugins: fastestmirror
     Loading mirror speeds from cached hostfile
     Resolving Dependencies
     --> Running transaction check
     ---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
     --> Finished Dependency Resolution
     ----------
     #此处省略二十多行输出语句
     ----------
     Installed:
       libaio.x86_64 0:0.3.109-13.el7                                                                                                    
    
     Complete!
    

    再执行

     [root@iZ286t0wuf9Z mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
     2018-11-21T07:30:42.888897Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.13) initializing of server in progress as process 26596
     2018-11-21T07:30:48.222050Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: l*JsMq=uX4>k
     2018-11-21T07:30:49.788339Z 0 [System] [MY-013170] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.13) initializing of server has completed
    

    再执行

     [root@iZ286t0wuf9Z mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
     [root@iZ286t0wuf9Z mysql]# chmod +x /etc/init.d/mysqld
    
  9. 配置环境变量

    在 /etc/profile 最后添加如下内容export PATH=$PATH:/usr/local/mysql/bin

     [root@iZ286t0wuf9Z mysql]# vim /etc/profile
    
     ----------
     #省略N行配置信息
     ----------
     unset i
     unset -f pathmunge
    
     #set mysql environment
     export PATH=$PATH:/usr/local/mysql/bin
    

    使配置文件生效

     [root@iZ286t0wuf9Z mysql]# source /etc/profile
    
  10. 启动 MySQL 服务

    [root@iZ286t0wuf9Z mysql]# /etc/init.d/mysqld start
    Starting MySQL.Logging to '/usr/local/mysql/data/iZ286t0wuf9Z.err'.
    .[  OK  ]
    [root@iZ286t0wuf9Z mysql]# 
    
  11. 设置 MySQL 服务开机启动

    此处根据参考文档用了 chkconfig 命令,但看到提示信息之后才知道,Centos 5.7开始,

    在 Centos 5.7 中服务不在是用 service 这个命令来启动与停止,也不再用 chkconfig 来设置开机启动与否!在 centos7 中所有对服务的管理都集中到了 systemctl 当中; systemctl 不再是合之前一样依赖 /etc/init.d/ 下的脚本,它是通过配置文件来完成对服务的管理的。(参考:蒋乐兴的技术随笔

    [root@iZ286t0wuf9Z ~]# chkconfig --list
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    aegis           0:off   1:off   2:on    3:on    4:on    5:on    6:off
    agentwatch      0:off   1:off   2:on    3:on    4:on    5:on    6:off
    netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
    network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
    

    此处暂且这样采用 chkconfig 设置

    [root@iZ286t0wuf9Z mysql]# chmod +x /etc/rc.d/init.d/mysqld
    [root@iZ286t0wuf9Z mysql]# chkconfig --add mysqld
    [root@iZ286t0wuf9Z mysql]# chkconfig --list mysqld
    [root@iZ286t0wuf9Z mysql]# chkconfig --level 35 mysqld on
    

    执行 add 操作之后,list 中就会有 mysqld 了

    [root@iZ286t0wuf9Z ~]# chkconfig --list
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    aegis           0:off   1:off   2:on    3:on    4:on    5:on    6:off
    agentwatch      0:off   1:off   2:on    3:on    4:on    5:on    6:off
    mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
    netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
    network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
    
  12. 初始化 root 密码

    并没意料到此处有障碍,本人没有找到初始密码在哪里,无奈选择重置。然后重置比我预计的复杂一点,步骤太多,请参考另一篇博客:MySQL 8.0 以上版本重置 root 用户密码

  13. 添加远程访问权限

    在服务器上可以登录 root 用户,但远程连连接会提示

    1130: host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server

    xxx.xxx.xxx.xxx 远程客户端的IP地址,即当前客户端IP地址禁止访问。

    [root@iZ286t0wuf9Z ~]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 8.0.13 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 host,user from user;
    +-----------+------------------+
    | host      | user             |
    +-----------+------------------+
    | localhost | mysql.infoschema |
    | localhost | mysql.session    |
    | localhost | mysql.sys        |
    | localhost | root             |
    +-----------+------------------+
    4 rows in set (0.00 sec)
    
    mysql> 
    

    查询结果显示,root允许的IP地址为localhost,修改成%即可。当然还有更复杂的设置,比如指定IP地址,或者指定IP区间,后续会再更新。

    mysql> update user set host='%' where user='root';
    Query OK, 1 row affected (0.03 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> 
    

    到这里就可以远程登录了。

<span id="jump">附:步骤4. 准备安装文件的过程截图</span> MySQL_01 MySQL_02 MySQL_03 MySQL_04 MySQL_05

先整理这么多了,欢迎大家共同讨论。

猜你喜欢

转载自my.oschina.net/u/3251146/blog/2907226