linux下安装MySQL8.0

linux下安装MySQL8.0

本次安装演示版本为8.0.28

1、准备安装目录及其安装包

cd /usr/local/
# 使用wget下载mysql安装包(如果下载慢,可自行提前准备安装包)
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
# 解压安装包
tar -xvf  mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz

rm -rf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz 

# 重命名
mv mysql-8.0.28-linux-glibc2.12-x86_64 mysql
# 切换到目录
cd mysql
# 准备数据存放data
mkdir data

2、准备用户组以及用户和密码并配置mysql环境变量

# 添加mysql组
groupadd mysql
# 添加mysql用户
useradd -g mysql mysql
# 给mysql安装目录配置权限
chown -R mysql.mysql /usr/local/mysql
chmod 750 /usr/local/mysql/data -R
# 编辑环境变量文件
vim /etc/profile
# 在环境变量末尾加上以下配置(mysql安装目录)
# export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib

3、编辑mysql配置文件

# 刷新环境变量
source /etc/profile
# 编辑配置文件
vim /etc/my.cnf
# 在配置文件中添加如下内容(去量覆盖)
[mysql]
# 默认字符编码
default-character-set=utf8mb4
[client]
# client默认端口
port=3306
# mysql以socket方式运行的sock文件位置
socket=/var/lib/mysql/mysql.sock

[mysqld]
# client默认端口
port=3306
# 用户名
user=mysql
# 开启mysql日志记录 (一般不会开启开功能,因为log的量会非常庞大。但个别情况下可能会临时的开一会儿general log以供排障使用。)
# general_log = 1
# 日志保存地址
general_log_file= /var/log/mysql/mysql.log
# mysql以socket方式运行的sock文件位置
socket=/var/lib/mysql/mysql.sock
# mysql安装地址
basedir=/usr/local/mysql
# mysql数据存放地址
datadir=/usr/local/mysql/data
# binlog日志名称前缀
log-bin=/usr/local/mysql/data/mysql-bin
innodb_data_home_dir=/usr/local/mysql/data
innodb_log_group_home_dir=/usr/local/mysql/data/
character-set-server=utf8mb4
# 忽略大小写
lower_case_table_names=1
# 默认认证插件
default_authentication_plugin=mysql_native_password

# 去掉登录时的密码校验
# skip-grant-tables

# 是否支持富豪链接
symbolic-links=0
# Disabling symbolic-links is recommended to prevent assorted security risks
# 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]
# mysql错误日志存放位置
log-error=/var/log/mariadb/mariadb.log
#mysql 运行pid存放位置
pid-file=/var/run/mariadb/mariadb.pid

# include all files from the config directory
# 导入其它配置
!includedir /etc/my.cnf.d

4、开始安装mysql

cd /var/log/
mkdir mysql
touch mysql.log
chown mysql /var/log/mysql/

# 切换bin目录(上面配置了环境变量,如若刷新环境变量也可不用切换)
cd /usr/local/mysql/bin

# 初始化mysql(如果后面操作错误可将mysql安装路径下的data目录删除后重新初始化)这一步会使mysql生成随机密码(可以不记,一般都需要改密码)
./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --initialize

# 切换目录
cd /usr/local/mysql
# 创建软链接
cp -a ./support-files/mysql.server /etc/init.d/mysql 
cp -a ./support-files/mysql.server /etc/init.d/mysqld

# 授权目录
chown mysql  /etc/my.cnf
chmod +x /etc/init.d/mysql
chmod +x /etc/init.d/mysqld

# 创建mysql启动的bin目录
mkdir /var/lib/mysql 
# 授权目录(mysql_safe的错误日志存放目录)
chown -R mysql:mysql /var/lib/mysql/ 
mkdir /var/log/mariadb
touch  /var/log/mariadb/mariadb.log
chown -R  mysql:mysql /var/log/mariadb/

5、启动mysql服务

# 启动mysql
service mysql start

# 其它常用命令
# 重启服务
service mysql restart
# 停止
service mysql stop
# 查看mysql状态
service mysql status

6、修改密码

# 编辑文件在配置文件中添加一行 skip-grant-tables
vim /etc/my.cnf
# 去掉登录时的密码校验
skip-grant-tables
# 重启mysql服务
service mysql restartcd
# 登录mysql,输入密码时直接回车
mysql -uroot -p
# 修改密码为空
mysql> use mysql;
mysql> update user set authentication_string = '' where user="root";
mysql> flush privileges;
mysql> quit
# 再次编辑文件在配置文件去掉 skip-grant-tables
vim /etc/my.cnf
# 编辑完成重启服务
service mysql restart
# 登录mysql,输入密码时直接回车(因为密码已经为空)
mysql -uroot -p
# 修改密码为自己所需要的密码
mysql> use mysql;
mysql> alter user 'root'@'localhost' identified by '你的密码';
mysql> flush privileges;
mysql> quit
posted @ 2022-11-12 16:34  橘子有点甜  阅读(583)  评论(0编辑  收藏  举报