linux安装mysql8.0.40
一、下载MySQL安装包
1.查看glibc版本
rpm -qa | grep glibc
2.到mysql官网下载安装包
二、解压安装
1.上传压缩包纸/usr/local 目录下,解压:
tar -xvf mysql-8.0.40-linux-glibc2.17-x86_64.tar.xz
2.重命名:
mv mysql-8.0.40-linux-glibc2.17-x86_64 mysql
3.创建数据初始化目录
mkdir -p /usr/local/mysql/data
4.检查mysql用户组和用户是否存在,如果没有,则创建
cat /etc/group | grep mysql
cat /etc/passwd |grep mysql
groupadd mysql
useradd -r -g mysql mysq
5.更改mysql目录下所有的目录及文件夹所属的用户组和用户,以及权限
chown -R mysql:mysql /usr/local/mysql
chmod -R 755 /usr/local/mysql
6.编辑配置文件my.cnf
vi /etc/my.cnf
添加配置如下:
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
# 允许最大连接数
max_connections=500
max_allowed_packet = 20M
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8mb4
lower_case_table_names=1
socket=/usr/local/mysql/data/tmp/mysql.sock # socket 文件路径
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[client]
default-character-set=utf8mb4
socket=/usr/local/mysql/data/tmp/mysql.sock # 客户端 socket 文件路径
7.编译安装并初始化mysql,务必记住初始化输出日志末尾的密码
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
8.添加软连接,并启动mysql服务
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
service mysql start
9.登录mysql,修改密码
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
flush privileges;
10.远程登录设置
update user set user.Host='%' where user.User='root';
flush privileges;
11.重启数据库
service mysql restart
12.设置开机自动启动
(1)将服务文件拷贝到init.d下
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
(2)赋予可执行权限
chmod +x /etc/init.d/mysqld
(3)添加服务
chkconfig --add mysqld
(4)显示服务列表
chkconfig --list