【记录】记录Linux安装Mysql8

192 阅读2分钟

下载mysql8 安装包 mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz downloads.mysql.com/archives/in…

检查是否已经安装过mysql    

rpm -qa | grep mysql

如果环境中有遗留mysql则执行删除命令:

rpm -e --nodeps mysql-xxxxxxxxx

查询遗留的mysql设置或命令,执行两条命令:

whereis mysql

find / -name mysql

如通过上述两条命令发现有遗留,则执行清除命令,将所有查到的mysql都删除感觉 :rm -rf  xxx xxx

查看是否存在mysql用户的命令:

cat /etc/group | grep mysql\
cat /etc/passwd |grep mysql

增加 mysql 用户的命令:

groupadd mysql\
useradd -r -g mysql mysql

解压安装包

tar -xvf mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz

移动文件夹路径并改名

mv mysql-8.0.11-linux-glibc2.12-x86_64 /usr/local/mysql

文件夹权限设定

chmod -R 755 /usr/local/mysql

设定一个数据存放位置

mkdir -p  /data/mysql

给该文件夹权限

chmod 777 /data/mysql    

编辑配置文件

/etc/my.cnf

将以下内容整个替换

[mysqld]
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# 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

port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
#socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
##character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true

[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
log-error=/data/mysql/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
pid-file=/data/mysql/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

初始化MySQL

cd /usr/lcoal/mysql/bin
./mysqld --initialize --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql

查看默认密码

cat /data/mysql/mysql.err

image.png 先将mysql.server放置到/etc/init.d/mysql中\

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

启动MySQL

service mysql start

image.png 进入MySQL内

./mysql -u root -p

修改密码和允许外部网络访问mysql (mysql8.0需要密码大于8位,大小写数字特殊字符)

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'youpassword'; 
FLUSH PRIVILEGES;
use mysql;
update user set host='%' where user='root';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'youpassword';

到此外部可以直接连接到服务器上的MySQL 最后 设置软连接

ln -s /usr/local/mysql/bin/mysql /usr/bin

将MySQL设置为开机启动

chkconfig --add mysql