下载地址
https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz
解压mysql,并移动到/usr/local,方便初始化mysql
tar -xvf mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz
mv mysql-8.0.15-linux-glibc2.12-x86_64 /usr/local/mysql
chown -R /usr/local/mysql #修改所属者与所属组
创建用户
useradd mysql -s /sbin/nologin
创建mysql存储数据目录,并修改所属者与所属组
mkdir /data
chown -R mysql.mysql /data
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data #初始化mysql,这一步会出现密码
创建mysql配置文件
vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data
port=3306
socket=/usr/local/mysql/mysql.sock
character-set-server=utf8
log-error=/data/mysqld/mysqld.log
pid-file=/data/mysqld/mysqld.pid
[mysql]
socket=/usr/local/mysql/mysql.sock
[client]
socket=/usr/local/mysql/mysql.sock
创建pid文件与log文件的存放目录,并修改所属者与所属组以及文件权限
mkdir /data/mysqld
chown -R mysql.mysql /data/mysqld
chmod 777 /data/mysqld
添加mysql到system管理软件中
vim /lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
TimeoutSec=0
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/data/mysqld/mysqld.pid $MYSQLD_OPTS
EnvironmentFile=-/etc/sysconfig/mysql
LimitNOFILE = 10000
Restart=on-failure
RestartPreventExitStatus=1
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false
添加mysql环境变量
sed -i '$a export PATH=/usr/local/mysql/bin:$PATH' /etc/profile
source /etc/profile
启动mysql以及设置开机自启
systemctl start mysqld
systemctl enbale mysqld
登录修改密码
mysql -uroot -p #填入初始化时出现的密码
alter user root@localhost identified by 'password'; #修改密码
添加可以远程连接的用户
create user 'root'@'%' identified by 'password';
grant all on *.* to 'root'@'%';
flush privileges;