Ubuntu18安装并配置mysql

1.安装mysql

`sudo apt update`
`sudo apt install mysql-server`

2.安装配置

sudo mysql_secure_installation

#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的选项)

#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)

#3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)

#4
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的选项)

#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)

#6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)
--------------------- 

Ubuntu18安装并配置mysql_第1张图片
Ubuntu18安装并配置mysql_第2张图片

3.检查mysql服务状态

systemctl status mysql.service

如果出现下图,说明是正常的
Ubuntu18安装并配置mysql_第3张图片

如果不行,启动mysql服务试试
sudo service mysql start

4.接下来设置连接mysql不使用sudo

首先,登录
sudo mysql -uroot

接下来的都是在mysql里敲得
查看当前用户
select user,host from mysql.user;
Ubuntu18安装并配置mysql_第4张图片

删除root账号
drop user root@localhost;

重新创建root
create user root@localhost identified by 'mysql';

授权:
grant all privileges on *.* to root@localhost with grant option;

刷新权限:
flush privileges;

退出mysql,重新连接:
mysql -uroot -pmysql

不用加sudo了,搞定

关于重置密码

host为%时允许远程登录

重置密码:
方法1
SET PASSWORD FOR root@'localhost' = PASSWORD('password');

方法2
UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';

方法3
USE mysql;
UPDATE user SET Password = PASSWORD('newpwd') WHERE Host = 'localhost' AND User = 'root';

你可能感兴趣的:(个人笔记)