文章目录
安装Mysql
sudo apt update
sudo apt upgrade
sudo apt install -y mysql-server
给root用户设置密码
#第一次安装完无需密码即可进入
sudo mysql -uroot
alter user 'root'@'localhost' identified with mysql_native_password by '123456';
exit #退出mysql
#再次进入就需要输入密码
sudo mysql -uroot -p
启动mysql查看监听ip
sudo systemctl start mysql.service
sudo netstat -tln
# 如果显示127.0.0.1:3306 就是本地监听呀,并未开放给外部访问。
修改默认端口和支持远程登录
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 将以下的字段改成这个值,port需要放开注释后修改端口
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
port = 3306
重启mysql查看监听ip
sudo service mysql restart
sudo netstat -tln
# 如果显示0.0.0.0:3306 就是开放给外部访问。
连接MySQL报错,is not allowed to connect to this MySQL server
问题描述:
本机装的MySQL数据库,本机可以正常连接,其他机器访问报错,is not allowed to connect to this MySQL server,防火墙等其他策略均配置没问题。出现该问题的原因是,MySQL数据库只允许自身所在的本机器连接,不允许远程连接。
# 在MySQL所在服务器上使用命令登录到MySQL数据库中
mysql -u root -p
# 选择mysql数据库,并查询权限
# 可以看到,执行查询语句后得到的数据结果中 host 的值是 localhost
use mysql;
select host from user where user='root';
# 执行update语句修改权限
update user set host = '%' where user ='root';
# 刷新配置
flush privileges;
# 再次执行查询权限语句
# 可以看到,执行查询语句后得到的数据结果中 host 的值是 %
select host from user where user='root';
# 最后再次用其他机器连接MySQL数据库,可以连接成功