ubuntu安装mysql,并启用远程连接
环境Ubuntu22.04 ,默认mysql安装版本为mysql8.0.27
1. 安装
apt update #如果下一步执行失败,先执行此命令,再重新执行
sudo apt install -y mysql-server
2. 初始登陆账号密码文件
/etc/mysql/debian.cnf
3. 修改初始密码,允许指定用户登陆
use mysql;
update user set host='%' where user='root'; #%通配符,表示IP,网段
alter user 'root'@'%' identified with mysql_native_password by 'mypassword';
grant all privileges on *.* to 'root'@'%'; #*通配符代指库.表;可选项:identified by 'newpassword' with grant option;
flush privileges;
4. 修改配置文件,对目标服务器开放端口
路径:/etc/mysql/mysql.conf.d/
systemctl restart mysql.service
5. 防火墙放行端口
sudo ufw allow 3306