linux安装mysql8

linux安装mysql8,第1张

安装mysql8

mysql8.x 源码安装
系统环境:CoentOS 8.2,mysql版本:8.0

准备工作

检查系统是否自带安装mysql

# 检查是否安装mysql
rpm -qa | grep mysql
# 如果安装了mysql,可以进行卸载
rpm -e mysql # 普通卸载模式
rpm -e --nodeps mysql ## 强力删除模式,如果使用这个命令删除时,提示有依赖其他的文件,则用该命令对其进行强力删除。

检查系统的 mysql 用户与组信息

# 检查是否存在mysql组
cat /etc/group | grep mysql
# 如果不存在则新增一个mysql组
groupadd mysql

# 检查mysql 用户密码
cat /etc/passwd | grep mysql
# 新增mysql用户密码
useradd -r -g mysql mysql
下载mysql包

下载mysql的源码安装包并上传至Linux的指定目录;如:/home/soft

下载地址页:https://dev.mysql.com/downloads/mysql/;下载选择系统Linux-Generic,选择适合的版本。

linux上通过wget的下载方式:

# wget是linux的命令,如需要安装
yum install wget
# 32位下载地址
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.29-linux-glibc2.12-i686.tar.xz
# 64位下载地址
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.29-linux-glibc2.12-x86_64.tar.xz

在windows或macOS上下载,然后通过ssh上传也可以。

以下载64位的安装包为例

下载之后,会得到一个压缩文件mysql-8.0.29-linux-glibc2.12-x86_64.tar.xz,并上传至/home/soft目录。

安装目录/usr/local/mysql-8.0

# 解压 mysql-8.0.29-linux-glibc2.12-x86_64.tar.xz
xz -d mysql-8.0.29-linux-glibc2.12-x86_64.tar.xz
tar -xvf mysql-8.0.29-linux-glibc2.12-x86_64.tar
# 将mysql-8.0.29-linux-glibc2.12-x86_64 移动至指定的安装目录
mv ./mysql-8.0.29-linux-glibc2.12-x86_64 /usr/local/mysql-8.0

安装配置

新建data文件夹

# 切换到mysql的安装目录
cd /usr/local/mysql-8.0
# 新建data文件夹
mkdir data
# 改变data的目录权限
chmod 755 -R /usr/local/mysql-8.0/data

用户授权

# 授权刚刚新建的mysql用户
chown -R mysql.mysql /usr/local/mysql-8.0
mysql初始化
# 切换mysql的bin目录
cd /usr/local/mysql-8.0/bin
# 执行mysql的初始化 *** 作
# mysql初始化的参数说明
# --lower_case_table_names=1 大小写不敏感 --lower_case_table_names=0 大小写敏感
#大小写敏感:
./mysqld --user=mysql --basedir=/usr/local/mysql-8.0/ --datadir=/usr/local/mysql-8.0/data/ --initialize
#大小写不敏感:
./mysqld --user=mysql --lower_case_table_names=1 --basedir=/usr/local/mysql-8.0/ --datadir=/usr/local/mysql-8.0/data/ --initialize
#得到临时密码;记得保存好临时密码,后续的步骤需要用到

编辑my.cnf文件

[mysqld]
port=3306
user=mysql
basedir=/usr/local/mysql-8.0
datadir=/usr/local/mysql-8.0/data
character-set-server=utf8mb4
lower_case_table_names=1
[mysqld_safe]
log-error=/usr/local/mysql-8.0/data/mysql.log
pid-file=/usr/local/mysql-8.0/data/mysql.pid

my.cnf其他配置信息

[mysql]
default-character-set=utf8mb4
[client]
#port=3306
socket=/var/lib/mysql/mysql.sock

[mysqld]
#port=3306
#server-id=3306
user=mysql
general_log = 1
general_log_file= /var/log/mysql/mysql.log
socket=/var/lib/mysql/mysql.sock
basedir=/usr/local/mysql/mysql-8.0
datadir=/usr/local/mysql/data
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
autocommit=1
default_authentication_plugin=mysql_native_password
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]
log-error=/usr/local/mysql/data/mysql.log
pid-file=/usr/local/mysql/data/mysql.pid

#
# include all files from the config directory

############### 客户端配置 ###############
[client]
port   = 3306
socket = /home/mysql/mysql-data/mysql.sock

############### 服务端配置 ###############
[mysqld]
port = 3306
autocommit     = ON
character-set-server   = utf8mb4
collation-server       = utf8mb4_general_ci
default-storage-engine = INNODB
basedir        = /home/mysql/mysql-install
datadir        = /home/mysql/mysql-data
tmpdir         = /home/mysql/mysql-data/tmp
socket         = /home/mysql/mysql-data/mysql.sock
pid-file       = /home/mysql/mysql-data/mysql.pid
log-error      = /home/mysql/mysql-data/log/error.log

#mysql8.0请在初始化前配上强制小写,否则后续配上会报错
lower_case_table_names = 1

[mysql]
#关闭自动补全sql命令功能
no-auto-rehash

添加mysql.server服务到系统

# 切换到 support-files 目录
cd /usr/local/mysql-8.0/support-files
cp -a ./mysql.server /etc/init.d/mysql

# 授权以及添加服务
chmod +x /etc/init.d/mysql
chkconfig --add mysql

启动和查看

# 启动mysql 
service mysql start
# 查看启动状态 
service mysql status
# 停止mysql服务
service mysql stop

将mysql命令添加到服务

ln -s /usr/local/mysql-8.0/bin/mysql /usr/bin
登录和修改密码
# 登录mysql密码使用之前随机生成的密码
# mysql -uroot -p
# 修改root密码  其中123456是新的密码自己设置
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

# 执行flush使密码生效
flush privileges;

# 选择mysql数据库
use mysql;
# 修改远程连接并生效 
update user set host='%' where user='root';
flush privileges;

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/939450.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-17
下一篇 2022-05-17

发表评论

登录后才能评论

评论列表(0条)

保存