Ubuntu下MySQL的安装及配置
更新源及安装mysql
sudo apt update
sudo apt install mysql-server
查看mysql运行状态
systemctl status mysql
修改密码
第一次进入无需密码登陆,提示输入密码直接回车
mysql -u root -p
alter user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
8.0.16之后增加了system_user权限,root默认是没有的,可以增加上
grant system_user on *.* to 'root';
新增用户
创建一个新用户并把权限都赋给它
create user '[用户名]'@'[localhost或者%]' IDENTIFIED WITH mysql_native_password BY '密码';
grant all on *.* to '[用户名]';
查看用户信息,其中host为%是可以远程访问,localhost只能本地访问
use mysql;
select host,user,plugin from user;
更新用户为可以远程访问并刷新权限
update user set host='%' where user='[用户名]';
flush privileges;
开启远程权限
修改mysql配置文件,让其可以远程访问
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
其中的bind-address默认为127.0.0.1,修改如下
bind-address = 0.0.0.0
注意:如果是云主机还需要在主机安全策略里面把3306端口开放