Linux CentOS 7.39 安装mysql8
1、新建mysql文件夹
数据比较大,所以我在服务器另外挂了一个盘装mysql,和默认安装一个道理,换路径即可
cd ../
//创建文件夹
mkdir mysql
//进入mysql文件夹
cd mysql
2、下载mysql8.0安装包并解压、重命名
//下载安装包
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
//解压安装包
tar xvJf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
//重命名解压后的文件夹
mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql-8.0
3、创建data文件夹和用户组并授权
//进入重命名后的文件夹
cd mysql-8.0/
//创建data文件夹
mkdir data
//创建用户组
groupadd mysql
//创建一个名为mysql的用户
useradd -g mysql mysql
//授权刚刚新建的用户
//将这些文件和目录的组所有权更改为组 mysql
chown -R mysql.mysql /cghome/mysql/mysql-8.0
//授权
chmod 750 /cghome/mysql/mysql-8.0/data -R
4、配置环境
//编辑/etc/profile文件
vim /etc/profile
//编辑保存后执行命令使配置文件生效
source /etc/profile
在最后新增一行,cat /etc/profile查看文件
//注意改为自己的路径
export PATH=$PATH:/cghome/mysql/mysql-8.0/bin:/cghome/mysql/mysql-8.0/lib
5、编辑my.cnf文件
注意:如果要数据库大小写在这里要提前增加配置lower_case_table_names = 1
//编辑my.cnf文件
vi /etc/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=/cghome/mysql/mysql-8.0
datadir=/cghome/mysql/mysql-8.0/data
log-bin=/cghome/mysql/mysql-8.0/data/mysql-bin
innodb_data_home_dir=/cghome/mysql/mysql-8.0/data
innodb_log_group_home_dir=/cghome/mysql/mysql-8.0/data/
# 服务端使用的字符集默认为UTF8
character-set-server=utf8mb4
#是否对sql语句大小写敏感,1表示不敏感
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=/cghome/mysql/mysql-8.0/data/mysql.log
pid-file=/cghome/mysql/mysql-8.0/data/mysql.pid
#
# include all files from the config directory
6、初始化 MySQL 数据库服务器
//初始化数据库
./mysqld --user=mysql --basedir=/cghome/mysql/mysql-8.0 --datadir=/cghome/mysql/mysql-8.0/data/ --initialize
//如果报错如下如,则顺序执行下面两行安装libaio库
1:yum install -y libaio.so.1
2:yum install -y libaio
//继续属性上面的初始化命令
./mysqld --user=mysql --basedir=/cghome/mysql/mysql-8.0 --datadir=/cghome/mysql/mysql-8.0/data/ --initialize
注意记录临时密码,后面登录时会用到
7、复制 mysql.server 文件,并授权
//返回mysql-8.0目录:/cghome/mysql/mysql-8.0
cd ../
//执行下面两行复制命令
1:cp -a ./support-files/mysql.server /etc/init.d/mysql
2:cp -a ./support-files/mysql.server /etc/init.d/mysqld
//授权,执行下面命令
1:chown 777 /etc/my.cnf
2:chmod +x /etc/init.d/mysql
3:chmod +x /etc/init.d/mysqld
//创建/var/lib/mysql文件,并授权
1: mkdir /var/lib/mysql
2:chown -R mysql:mysql /var/lib/mysql/
8、启动mysql
//启动
service mysql start
9、登录mysql
//登录mysql
mysql -u root -p
//如下图:并输入上面的临时密码回车登录
10、修改密码,打开远程访问
//修改root密码并刷新
1:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'AAAAbbb2024'
2:flush privileges
//切换数据库,打开远程访问
use mysql
//更改root用户访问的限制、
update user set host='%' where user='root' and host='localhost'
//刷新
flush privileges
11、防火墙开放3306端口
//开启防火墙
systemctl start firewalld
//防火墙中开启3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
//重新加载防火墙的规则
firewall-cmd --reload
//查看端口开放
firewall-cmd --zone=public --list-ports
完活!