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 (我的選項(xiàng))
#2
Please set the password for root here...
New password: (輸入密碼)
Re-enter new password: (重復(fù)輸入)
#3
By default, a MySQL installation has an anonymous user,allowing anyone to log into MySQL without having to havea user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的選項(xiàng))
#4
Normally, root should only be allowed to connect from'localhost'. This ensures that someone cannot guess atthe root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的選項(xiàng))
#5
By default, MySQL comes with a database named 'test' thatanyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的選項(xiàng))
#6
Reloading the privilege tables will ensure that all changesmade so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的選項(xiàng))?


3、檢查mysql服務(wù)狀態(tài)
systemctl status mysql.service
如果出現(xiàn)下圖,說明是正常的:

如果不行,啟動(dòng)mysql服務(wù)試試:
sudo service mysql start
4、設(shè)置連接mysql不使用sudo
首先,登錄:
sudo mysql -uroot
接下來的都是在mysql里敲得。
查看當(dāng)前用戶:
select user,host from mysql.user;

刪除root賬號:
drop user root@localhost;
重新創(chuàng)建root:
create user root@localhost identified by 'mysql';
授權(quán):
grant all privileges on *.* to root@localhost with grant option;
刷新權(quán)限:
flush privileges;
退出mysql,重新連接:
mysql -uroot -pmysql;
不用加sudo了,搞定。
5、重置密碼的方法
SET PASSWORD FOR root@'localhost' = PASSWORD('newpassword');
6、在Ubuntu18.04系統(tǒng)下徹底刪除MySQL的方法
轉(zhuǎn)載:Ubuntu18安裝并配置mysql