centos7安装mysql8

阿里云服务器中 mysql 5.6版本中无法使用 json_object函数,从官网手动下载mysql8进行安装

1. 安装 MySQL5.6

MySQL5.6.30

2. 下载 MySQL 8.0.xx

https://downloads.mysql.com/archives/community/

选择操作系统为 Linux-Generic

下载 .tar.xz 文件

传输文件到 /app/mysql,进行解压

1
2
3
[root@centos7 ~]# cd /app/mysql
[root@centos7 mysql]# tar -xvJf mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz
[root@centos7 mysql]# mv mysql-8.0.30-linux-glibc2.12-x86_64 mysql-8

3. 安装MySQL

创建一个mysql用户:

1
[root@centos7 mysql]# useradd mysql

设置mysql用户密码:

1
2
3
[root@centos7 mysql]# echo '123456'|passwd --stdin mysql
更改用户 mysql 的密码 。
passwd:所有的身份验证令牌已经成功更新。

进入到mysql的bin目录下 新建data,log文件夹

1
2
3
4
5
6
[root@centos7 mysql]# cd mysql-8
[root@centos7 mysql-8]# mkdir data
[root@centos7 mysql-8]# mkdir log
[root@centos7 mysql-8]# chown -R root:root ./
[root@centos7 mysql-8]# chown -R mysql:mysql data
[root@centos7 mysql-8]# chown -R mysql:mysql log

建立mysql软链接

1
2
[root@centos7 mysql-8]# rm -rf /usr/bin/mysql
[root@centos7 mysql-8]# ln -s /app/mysql/mysql-8/bin/mysql /usr/bin/mysql

复制 /etc/init.d/mysql 配置文件

1
2
[root@centos7 mysql-8]# rm -rf /etc/init.d/mysql
[root@centos7 mysql-8]# cp support-files/mysql.server /etc/init.d/mysql

然后在/etc/init.d/mysql 中配置

1
[root@centos7 mysql-8]# vi /etc/init.d/mysql

添加两行,就是mysql安装目录和目录下data

1
2
basedir=/app/mysql/mysql-8
datadir=/app/mysql/mysql-8/data

新建 /etc/my.cnf - 该文档来源于 mysql-5.6.30/support-files/my-default.cnf

1
2
[root@centos7 mysql]# rm -f /etc/my.cnf
[root@centos7 mysql]# vi /etc/my.cnf

设置数据存放目录,日志存储目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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 = /app/mysql/mysql-8
datadir = /app/mysql/mysql-8/data
character-set-server = UTF8MB4
port = 3306
# server_id = .....
# socket = .....

# 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

log_error = /app/mysql/mysql-8/log/error.log

初始化 mysqld

1
2
[root@centos7 mysql-8]# cd bin 
[root@centos7 bin]# ./mysqld --initialize --console --user=mysql --basedir=/app/mysql/mysql-8 --datadir=/app/mysql/mysql-8/data

此时在 /app/mysql/mysql-8/log 中存在 error.log 里面存放着mysql登录的初始密码

1
[Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 0?z0<n#6qbU5

正常启动mysql服务

1
2
[root@centos7 mysql-8]# service mysql start
Starting MySQL. [ OK ]

4. 系统控制mysql

1
2
3
4
5
6
7
8
# 启动mysql命令
service mysql start

#关闭mysql命令
service mysql stop

#查看运行状态命令
service mysql status

5. 修改密码

输入的是刚刚的密码 0?z0<n#6qbU5

1
2
[root@centos7 mysql-8]# mysql -uroot -p
Enter password:

执行修改密码的命令

出现错误❌

mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory

MySQL 采用的是 Linux- Generic 包安装

解决问题

1
sudo ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5

问题解决后,更新密码

1
2
mysql> alter user 'root'@'localhost' identified by '密码';
Query OK, 0 rows affected (0.01 sec)

6. 配置远程访问

1
2
3
4
5
mysql> create user 'root'@'%' identified by '密码';
Query OK, 0 rows affected (0.02 sec)

mysql> grant all on *.* to 'root'@'%';
Query OK, 0 rows affected (0.01 sec)

现在可以通过 Navicat 连接 ecs

本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2021/08/01/centos7安装mysql8/
  • 发布时间: 2021-08-01 19:25
  • 更新时间: 2023-04-15 16:17
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!