去mysql官网获取下载链接
下载
wget -P /usr/local/download https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz
解压,移动
tar -zxvf mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz
mv /usr/local/download/mysql-8.0.11-linux-glibc2.12-x86_64 /usr/local/mysql
添加默认配置文件
vim /etc/my.cnf
[client]
port=3306
socket=/tmp/mysql/mysql.sock
[mysqld]
port=3306
user=mysql
socket=/tmp/mysql/mysql.sock
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=error.log
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
创建mysql组,mysql用户
groupadd mysql
useradd -g mysql mysql
初始化mysql
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
注意记录mysql初始密码。这边不记的话后面在日志中可以查看。我这里是J_SnHdBpp6zF
遇到错误
./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决方法
yum install -y libaio
建立MySQL服务
cd /usr/local/mysql
cp -a ./support-files/mysql.server /etc/init.d/mysqld
添加到系统服务
cp -a ./support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
配置全局环境变量
编辑/etc/profile文件
vi /etc/profile
在 profile 文件底部添加如下两行配置,保存后退出
PATH=/data/mysql/bin:/data/mysql/lib:$PATH
export PATH
设置环境变量立即生效
source /etc/profile
启动MySQL服务
service mysql start
给文件赋权。
chown -R mysql:mysql /var/log
chmod -R 777 /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/run
chmod -R 777 /var/run/mariadb/mariadb.pid
chown -R mysql:mysql /tmp/mysql
chmod -R 777 /tmp/mysql/mysql.sock
chown -R mysql:mysql /usr/local/mysql/data
chmod -R 777 /usr/local/mysql/data
使用初始密码登录。
cd /usr/local/mysql/bin
./mysql -uroot -p
Enter Password:J_SnHdBpp6zF
登录的时候输入密码报错。 ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
经研究发现。mysql8.0的加密方式与5.7不同。阿里云的云镜像没有这个插件。
修改配置文件/etc/my.cnf。使用老的验证方式。如图,加入default-authentication-plugin=mysql_native_password
改完以后我重启服务没用,然后reboot了服务器才生效的。
登进去改密码。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
FLUSH PRIVILEGES;
授权 root 用户的所有权限并设置远程访问
use mysql;
select host,user,authentication_string,plugin from user;
update user set host = "%" where user = "root";
flush privileges;
接下来就可以开启3306端口了,centos7.4需要装一下iptables
1.安装或者更新iptables-services
yum install iptables-services;
2.启动iptables
systemctl enable iptables
这个时候,/etc/sysconfig目录下面就有了iptables文件了。
3.打开iptables
systemctl start iptables
4.编辑
vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
5.重启防火墙
service iptables restart
如果用的阿里云的话这里还要在阿里云后台配一下安全组规则