mysql 配置远程连接
1. mysql安装和配置
sudo apt-get install mysql-server-5.7
systemctl status mysql
service mysql status
修改mysql的配置文件:
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
以下为mysqld.cnf文件主要内容,这里的skip-grant-tables 用于跳过验证直接登录
如果是设置default_authentication_plugin=mysql_native_password,则必须在登录时存在密码
bind-address = 0.0.0.0,这个值如果设置为0.0.0.0则是设置了支持外部连接,也就是使用其静态IP,如果设置为127.0.0.1,则只能由本地连接。
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
#
# * Basic Settings
#
default_authentication_plugin=mysql_native_password
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#skip-grant-tables
bind-address = 0.0.0.0
#
# * Fine Tuning
#
key_buffer_size = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options = BACKUP
#max_connections = 100
#table_open_cache = 64
#thread_concurrency = 10
#
# * Query Cache Configuration
#
query_cache_limit = 1M
query_cache_size = 16M
log_error = /var/log/mysql/error.log
expire_logs_days = 10
max_binlog_size = 100M
2. mysql设置跳过验证
- 删除skip-grant-tables 前面的#,并且注释掉default_authentication_plugin=mysql_native_password,然后通过“systemctl restart mysql 用于跳过验证直接登录,
3. mysql修改密码和配置IP
在mysql5.7,修改密码的方式为:
执行2中的跳过验证直接登录root
mysql -u root -p
# 无需输入密码
mysql>use mysql
mysql> update user set authentication_string=password("123456") ,plugin='mysql_native_password' where user='root';
# 其中的plugin='mysql_native_password',设置了激活root的密码并且设置为123456
mysql> flush privileges;
mysql>exit
systemctl restart mysql
# 修改mysql.cnf文件内容,改回不跳过输入密码,
# skip-grant-tables
systemctl restart mysql
# 重启后执行
mysql -u root -p
此时会提示输入密码,输入123456后正常登录,输入其他无法登录
- 检查当前用户名情况:select user,plugin from user;
在之前版本设置密码方式为:
UPDATE user SET Password = PASSWORD('newpassword') WHERE user = 'root';
# 在mysql5.7中,因为没有password 字段,所以会报错
- 查看user 和允许连接的IP
mysql>select user,host from user;
host处如果为localhost时只允许本地使用,改成 % 即可远程使用:
update user set host='%' where user='root';
# 查看
select user,host from user;
4. 远程连接mysql
选择sqlyog,设置IP:192.168.8.110,用户名root,密码123456,端口3306 即可登录,不过,在此之前如果没有以上的设置,这样登录才正常,否则会报各种错误
比如如果没有设置bind-address,就会因为无法找到这个IP而错无号码2002
参考
https://blog.csdn.net/Dwj1212/article/details/123451649