装好MySQL服务器端和客户端。登入数据库是需要密码的,在/etc/mysql/debian.cnf里面可以查看用户和密码,可以登入数据库。
sudo cat /etc/mysql/debian.cnf
会得到类似如下结果
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = rdaY5uYNptYFyinE
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = rdaY5uYNptYFyinE
socket = /var/run/mysqld/mysqld.sock
用上面的user和password就可以登入数据库
修改数据可中root用户的密码
首先了解一些MySQL的加密后的密码存在数据库mysql中的user表中的authentication_string这个字段中,登入数据库后,use mysql
进入存储相关信息的数据库,可以select user, authentication_string from user;
查看用户及加密后的密码,这里可以看到上面的debian-sys-maint用户,可以测试一下加密后的密码是不是和authentication_string字段中存的一样
select password('rdaY5uYNptYFyinE');
通过对比我们发现,authentication_string确实是存储加密后的密码的字段,password()函数为MySQL中的不可逆的加密函数,下面将如何设置root的密码
修改root的plugin字段,plugin root的字段是auth_socket,我们改掉它为mysql_native_password
update user set authentication_string=password("密码字符串"),plugin='mysql_native_password' where user='root';
退出,重启,登录
不用密码登入的操作
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
进入到这个配置文件,然后在这个配置文件中的[mysqld]这一块内容中加入skip-grant-tables这句话:
退出并重启后就可以不用密码登录了。
远程不能访问数据库问题
-
查看数据库用户host列是localhost还是%,必须改为%;
-
把/etc/my.cnf中的 bind-address = 127.0.0.1前加一个#注释掉;
-
如果开了防火墙,注意看3306端口是不是打开了;