Centos7安装Mysql8记录

Centos7安装Mysql8记录

本文主要记录在Centos7上安装Mysql8的过程步骤以及其中遇到的问题。

环境介绍

  • Linux系统版本:CentOS-7-x86_64
  • 数据库版本 :mysql-8.0.12-linux-glibc2.12-x86_64

Mysql8安装步骤

安装包下载(可自行下载相应版本)

安装包解压

  • 下载的Mysql为.tar.xz的包,使用以下命令先进行解包:
$ xz -d mysql-8.0.12-linux-glibc2.12-x86_64.tar.xz
  • 解包后得到.tar包,继续解压缩:
$ tar -zxvf mysql-8.0.12-linux-glibc2.12-x86_64.tar

修改配置文件

本机上mysql的安装目录为: opt/mysql-8.0.12/

$ vim /etc/my.cnf

添加以下配置

#客户端相关配置
[client]
#客户端连接端口
port=3306
#客户端连接socket,必须与服务端的socket使用同一个
socket=/opt/mysql-8.0.12/socket/mysql.sock
#服务端相关配置
[mysqld]
#mysql的安装目录
basedir=/opt/mysql-8.0.12
#mysql数据存储目录
datadir=/opt/mysql-8.0.12/data
#服务端socket存放目录
socket=/opt/mysql-8.0.12/socket/mysql.sock
symbolic-links=0
[mysqld_safe]
#错误日志打印
log-error=/opt/mysql-8.0.12/mariadb/mariadb.log
#pid存放文件目录
pid-file=/opt/mysql-8.0.12/mariadb/mariadb.pid

/opt/mysql-8.0.12/可以修改为自己mysql的安装目录,配置中不存在的目录可以自己新建,然后修改目录权限,如:data、socket、mariadb等目录都可以自己创建,然后修改目录权限用户和组为mysql

添加mysql组与mysql用户

创建mysql组

$ groupadd mysql

创建mysql用户

$ useradd -g mysql mysql

初始化mysql

$ /opt/mysql-8.0.12/bin/mysqld --initialize --user=mysql --basedir=/opt/mysql-8.0.12/ --datadir=/opt/mysql-8.0.12/data/

初始化过程中如果出现错误,很大可能是目录权限的问题,可使用以下命令进行相应目录权限的修改:

$ chown -R mysql:mysql /opt/mysql-8.0.12/socket

正常无异常的情况下执行初始化命令后,会打印如下日志,从日志中可以看到mysql默认生成root账号和密码 root@localhost: _gxKig5yyUZa

2018-08-27T09:15:56.466491Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2018-08-27T09:15:56.466661Z 0 [System] [MY-013169] [Server] /opt/mysql-8.0.12/bin/mysqld (mysqld 8.0.12) initializing of server in progress as process 2328
2018-08-27T09:15:59.488585Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: _gxKig5yyUZa
2018-08-27T09:16:00.995381Z 0 [System] [MY-013170] [Server] /opt/mysql-8.0.12/bin/mysqld (mysqld 8.0.12) initializing of server has completed

启动mysql服务

./opt/mysql-8.0.12/support-files/mysql.server start|stop|restart(启动|停止|重启)

启动过程中出现如下异常

Starting MySQL.2018-08-27T09:00:31.110644Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
 ERROR! The server quit without updating PID file (/opt/mysql-8.0.12/data/master.pid).
Starting MySQL.2018-08-27T09:10:37.294456Z mysqld_safe error: log-error set to '/opt/mysql-8.0.12/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
 ERROR! The server quit without updating PID file (/opt/mysql-8.0.12/data/master.pid).

可以修改或创建相应的配置目录或文件,并修改目录的组和用户为mysql,即可解决以上异常。
看到以下日志打印,表示mysql服务启动成功。

Starting MySQL.. SUCCESS!

使用mysql客户端连接mysql服务

./opt/mysql-8.0.12/bin/mysql -uroot -p

输入上面数据库初始化是生成的root账户的密码:_gxKig5yyUZa

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.12
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>

修改root账户的默认密码

使用root账户以及初始化密码首次登陆成功后,mysql会要求你修改初始化密码

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

在mysql客户端连接下进行root账户默认密码的修改:

mysql> set password='123456';

修改允许远程访问本机的mysql服务,并修改密码的校验方式:
修改前默认的校验方式是caching_sha2_password:

mysql> select u.host, u.user, u.plugin from mysql.user u;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| localhost | root             | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
+-----------+------------------+-----------------------+

修改host为“%”,否则会报"1130"的错误
修改为mysql_native_password进行密码验证,否则远程连接会报"2059"的错误

mysql> update mysql.user set host='%' where user='root';
mysql> alter user 'root'@'%' identified with mysql_native_password by '123456';
mysql> flush privileges;
mysql> select u.host, u.user, u.plugin from mysql.user u;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
+-----------+------------------+-----------------------+

结束安装

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值