1. 程式人生 > >Ubuntu18安裝並配置mysql

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 (我的選項)
--------------------- 

在這裡插入圖片描述 插入圖1,2

3.檢查mysql服務狀態

systemctl status mysql.service

如果出現下圖,說明是正常的 插入圖3

如果不行,啟動mysql服務試試 sudo service mysql start

4.接下來設定連線mysql不使用sudo

首先,登入 sudo mysql -uroot

接下來的都是在mysql裡敲得 檢視當前使用者 select user,host from mysql.user; 插入圖4

刪除root賬號 drop user [email protected];

重新建立root create user [email protected] identified by 'mysql';

授權: grant all privileges on *.* to [email protected] with grant option;

重新整理許可權: flush privileges;

退出mysql,重新連線: mysql -uroot -pmysql

不用加sudo了,搞定

關於重置密碼

host為%時允許遠端登入

重置密碼: 方法1 SET PASSWORD FOR [email protected]'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';