ubuntu18.04下安装mysql5.7+及创建用户
2019-01-18 本文已影响0人
Zywg皓
mysql5.7+安装与之前版本有一些不同,参考老的教程容易踩坑,所以我总结了一下供大家参考
基本命令:
sudo apt update
sudo apt install mysql-server
sudo netstat -tap | grep mysql
与之前版本不同,5.7+安装过程中不会让你输入root密码,所以应该如何登陆mysql?
1.你是用root账户登录的
那么简单一条命令即可mysql -u root
不需要输入密码。

2.你不是root账户登录的
那么方法一就行不通了,mysql会拒绝你登录。

对于这种情况网上很多教程是采用免密模式登录mysql,然后修改密码。但其实有一种更简单的方式:
打开下面的文件sudo vim /etc/mysql/debian.cnf
里面有mysql提供的默认账户密码。
然后登录mysql -u debian-sys-maint -p


现在登录mysql的问题解决了,但是还有个问题,为什么root用户可以免密登录而其他用户不行?
查看mysql下的user表我们会发现:root账户是用auth_socket登录的,密码为空,所以你是无法用密码登录的。对于auth_socket我查到的解释是:如果连接是通过unix套接字完成的,并且客户端进程的所有者与连接到mysqld时使用的用户名匹配,则身份验证成功。
所以需要root账户才能免密登录mysql。

然后创建一个名为ubuntu,密码为NewPassword,Host为localhost的mysql用户:
CREATE USER 'ubuntu'@'localhost' IDENTIFIED BY 'NewPassword';
GRANT ALL PRIVILEGES ON *.* TO 'ubuntu'@'localhost';
FLUSH PRIVILEGES;
或者修改root密码及登录方式:
UPDATE mysql.user SET authentication_string=PASSWORD('NewPassword'), plugin='mysql_native_password' WHERE User='root' AND Host = 'localhost';
FLUSH PRIVILEGES;
之后便可用账号密码登录了。