vagrant@ubuntu-bionic:~$ mysql -uroot -p Enter password: mysql> use mysql; mysql> CREATE USER 'remoteuser'@'%' IDENTIFIED BY 'iamsuperman'; mysql> GRANT ALL ON *.* TO 'remoteuser'@'%'; mysql> ALTER USER 'remoteuser'@'%' IDENTIFIED WITH mysql_native_password BY 'iamsuperman'; #如果你想只允许特定的Ip连接MySQLServer,那么可以这么写 #mysql> ALTER USER 'remoteuser'@'{指定ip}' IDENTIFIED WITH mysql_native_password BY 'iamsuperman'; mysql> FLUSH PRIVILEGES; mysql>exit (base) vagrant@ubuntu-bionic:~$ sudo service mysql restart
重启MySQLServer后,我们发现下图情况,表示我们可以使用remoteuser进行远程连接。
由于我们我们执行了 GRANT ALL ON . TO 'remoteuser'@'%'; 语句,所以remoteuser等同于root用户,拥有所有权限
$ mysql mysql> use mysql; mysql> select authentication_string from user where user='root'; # 我们会看到 +------------------------------------------------------------------------+ | authentication_string | +------------------------------------------------------------------------+ | $A$005$xQI&%/b/]=h5BM8FODO3pX6gSlaFCv9tJkUgt9bMFAZLVDYJqAAcGeaLA | +------------------------------------------------------------------------+ # authentication_string就是加密后的密码,但是却不能直接修改成我们想要的密码。 # 这里我们先将该值置空,然后取消skip-grant-tables,再重启,登陆,修改密码 mysql> update user set authentication_string = ''where user = 'root'; mysql> quit $ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf # 删除之前加在末尾的 skip-grant-tables $ sudo service mysql restart $ mysql -u root -p //提示输入密码时直接敲回车。 mysql> use mysql; mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码'; # 退出,重启MySQL即可。至此MySQL密码重