参考博客
https://blog.csdn.net/m0_70556273/article/details/126490767
https://blog.csdn.net/qq_61672548/article/details/125629138
https://www.runoob.com/note/40579
https://blog.csdn.net/qq_35124072/article/details/122867295
1、下载mysql 8.X
官网下载 https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz
约800M大小
2、创建文件夹并解压
mkdir /use/local/mysql
tar xvJf mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz
3、移动文件到mysql文件下
mv mysql-8.0.25-linux-glibc2.12-x86_64 mysql-8.0
4、/use/local/mysql文件夹下创建data文件夹 存储文件
mkdir data
5、分别创建用户组以及用户
groupadd mysql
useradd -g mysql mysql
6、授权用户
chown -R mysql.mysql /usr/local/mysql/mysql-8.0
chmod 750 /usr/local/mysql/data -R
7、配置全局mysql命令(可能需要重启系统才能生效)
vim /etc/profile
在最后一行添加
export PATH=$PATH:/usr/local/mysql/mysql-8.0/bin:/usr/local/mysql/mysql-8.0/lib
8、编辑mysql配置
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=/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
9、运行初始化mysql
cd /usr/local/mysql/mysql-8.0/bin
./mysqld --user=mysql --basedir=/usr/local/mysql/mysql-8.0 --datadir=/usr/local/mysql/data/ --initialize
这步可能报错:data文件夹内存在文件,则先删除之前创建的data文件夹
rm -rf /use/local/mysql/data
也有可能报错:没有找到命令libaio,需要先安装libaio
yum install -y libaio.so.1
yum install -y libaio
运行无任何error则进行下一步
10、复制 mysql.server 文件,在/usr/local/mysql/mysql-8.0目录下执行
cd /usr/local/mysql/mysql-8.0
cp -a ./support-files/mysql.server /etc/init.d/mysql
cp -a ./support-files/mysql.server /etc/init.d/mysqld
11、赋予权限
chown 777 /etc/my.cnf
chmod 777 /etc/init.d/mysql
chmod 777 /etc/init.d/mysqld
12、创建mysql.log
cd /var/log
mkdir mysql
cd mysql/
touch mysql.log
chmod 777 /var/log/mysql/mysql.log
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql/
13、启动服务
service mysql start
出现success则成功
以后重启mysql命令: /etc/init.d/mysqld restart
关闭: /etc/init.d/mysqld stop
14、修改mysql root用户密码
修改my.cnf,使得可以无密码登录
vim /etc/my.cnf
在[mysqld]下面增加一行:
skip-grant-tables
15、重启mysql服务,使配置生效
/etc/init.d/mysqld restart
16、进入mysql
执行命令
mysql
进入之后使用mysql表
use mysql;
将root用户密码置空
update user set authentication_string = '' where user = 'root';
退出mysql
exit;
17、将第14步的修改注释
vim /etc/my.cnf
注释掉: #skip-grant-tables
重启服务
/etc/init.d/mysqld restart
18、登录mysql 不需要输入密码
mysql -u root -p
(不要输入密码,直接敲回车)
修改密码
ALTER user 'root'@'localhost' IDENTIFIED BY '你的密码';
FLUSH PRIVILEGES;
19、结束