环境介绍
操作系统:Centos7.6
My SQL版本:8.0.30
本文使用的是当前最新8.0.30版本,如果需要安装的版本比8.0.30高或者低,只要是8.0.*版本,那就可以按照本文说明安装,基本不会有任何问题。
一、安装准备
1.卸载MariaDB
安装My SQL的话会和MariaDB的文件冲突,所以需要先卸载掉MariaDB。
1.1查看是否安装mariadb
1.2 卸载
1.3 检查是否卸载干净
2.检查依赖libaio(异步读写)
如果没有安装则执行(安装libaio)
yum -y install libaio
如果没有安装则执行(安装numactl)
yum -y install numactl
二、安装MySQL
1.下载资源包
cd /usr/local
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.30-el7-x86_64.tar
2.解压
注:本文安装包上传到了 /usr/local/ 目录下
进入安装包目录
cd /usr/local
拆分tar包和
tar -xvf mysql-8.0.30-el7-x86_64.tar
解压安装包
tar -zxvf mysql-8.0.30-el7-x86_64.tar.gz
3.重命名
3.1将解压后的文件夹重命名为mysql
mv mysql-8.0.30-el7-x86_64/ mysql
4.创建存储数据文件
mkdir mysql/data
5.设置用户组并赋权
groupadd mysql
5.2创建用户
-r:创建系统用户
-g:指定用户组
useradd -r -g mysql mysql
5.3更改属主和数组
chown -R mysql:mysql /usr/local/mysql/
5.4更改权限
chmod -R 755 /usr/local/mysql/
6.初始化MySQL进入MySQL的bin目录
cd /usr/local/mysql/bin/
初始化
../mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
7.配置参数文件
nano /etc/my.cnf
[client]
port = 3306
socket = /usr/local/mysql/data/mysql.sock
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect = 'SET NAMES utf8mb4'
port = 3306
socket = /usr/local/mysql/data/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
datadir = /usr/local/mysql/data
#lower_case_table_names=1
#如果要设置lower_case_table_names可以在初始化里面设置 ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --lower_case_table_names=1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
设置my.cnf权限
配置后修改 /etc/my.cnf 的权限为644
8.启动MySQL
/usr/local/mysql/support-files/mysql.server start
9.设置软连接,并重启MySQL
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql/mysql.sock /var/mysql.sock
service mysql restart
10.登录并更改密码
mysql -uroot -p
Enter password: 输入初始化随机密码
两种改密方式二选一
alter user 'root'@'localhost' identified by '123456';
set password for root@localhost = '123456';
11.开放远程连接
mysql>use mysql;
msyql>update user set user.Host='%' where user.User='root';
mysql>flush privileges; //刷新权限