1.下载及安装mysql
- 下载,去http://dev.mysql.com/downloads/repo/yum/
找到最新的版本,8.0,下载安装用的Yum Repository
cd /usr/local/src
wget -i -c https://repo.mysql.com//mysql80-community-release-el8-1.noarch.rpm
(新版本地址:https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-community-server-8.0.19-1.el8.x86_64.rpm)
- yum安装mysql源,自动
yum -y install mysql80-community-release-el8-1.noarch.rpm
-
检查安装情况
- yum安装mysql服务器,自动
yum -y install mysql-server
- 安装完成后,配置文件在/etc/my.cnf
2.mysql 数据库配置
- 启动mysql
systemctl start mysqld.service
- 查看运行状态
systemctl status mysqld.service
这样代表启动成功
- 找到mysql的默认密码,然后登陆,修改密码(必做)
cat /var/log/mysql/mysqld.log |grep "password"
可以看到mysql8默认密码为空,所以直接登陆修改密码
- 直接登陆修改密码
mysql -u root
alter user 'root'@'localhost' identified by 'mysql';
flush privileges;
exit;
3. 允许远程连接
3.1在操作系统层面操作,检查3306端口是否开放
-
在远程开发端,用telnet访问:
说明防火墙把3306端口屏蔽了,centos8不在使用iptables,默认使用firewall作为防火墙,查看一下firewall的状态:
systemctl status firewalld
- 进入firewall的配置目录,把3306端口加入白名单:
cd /etc/firewalld/zones
vi public.xml
或者直接使用命令加白名单:
firewall-cmd --add-port=3306/tcp --permanent
- 重载防火墙
firewall-cmd --reload
3.2在mysql中设置,允许其他ip对mysql的访问
划重点:新版的mysql中,把创建用户权限记录和授权分开了,不能同时做,以前用以下命令做可以,但是新版mysql报错(起码mysql8会报错)
grant all on . to root@'%' identified by 'mysql' with grant option; *
mysql> GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY 'mysql' WITH GRANT OPTION;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'mysql' WITH GRANT OPTION' at line 1
正确做法如下:
1)创建所有ip的root权限记录
create user 'root'@'%' identified by 'mysql';
2)然后授权(grant 权限列表 on 数据库 to '用户名'@'访问主机' ;(修改权限时在后面加with grant option))
grant all privileges on *.* to 'root'@'%' with grant option;
3)刷新权限(mysql8貌似不需要这一步也可以)
flush privileges
客户端访问效果: