ubuntu20.04安裝mysql8

方法一夭咬、使用源 安裝
1、使用命令安裝mysql

#更新源
sudo apt-get update
#安裝msql-server
sudo apt-get install mysql-server

2赃额、初始化配置

安裝的是最新版本mysql8.x

安裝完成后加派,現(xiàn)在啟用 MySQL(系統(tǒng)啟動時自動啟動),啟動 MySQL跳芳,并使用以下命令驗證狀態(tài):

systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld

在初始化配置(sudo mysql_secure_installation)前先設(shè)置root密碼芍锦,否則會報以下錯:

Re-enter new password: 
 ... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.
root@myubuntu:~# sudo mysql
mysql> alter user 'root'@'localhost' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.00 sec)

#退出
mysql> quit;
Bye

然后再初始化配置:

sudo mysql_secure_installation

1

ubuntu@i-kh37hnyf:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: Y(選擇N ,不會進行密碼的強校驗)

2

#已經(jīng)設(shè)置過密碼了,這步會直接略過飞盆!
Please set the password for root here...
New password: (輸入密碼)
Re-enter new password: (重復輸入)

3

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2

4

Using existing password for root.
Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N
 ... skipping.

5

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. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N(選擇N娄琉,不刪除匿名用戶)
 ... skipping.

6

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) : N(選擇N,允許root遠程連接)
 ... skipping.

7

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N(選擇N吓歇,不刪除test數(shù)據(jù)庫)
 ... skipping.

8

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(選擇Y孽水,修改權(quán)限立即生效)
Success.

All done! 

3、配置root遠程登錄
3.1 修改bind-address城看,默認mysql只能本地訪問

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
 #找到 bind-address: 127.0.0.1 并注釋掉

#重啟mysql
sudo /etc/init.d/mysql restart

3.2 root登錄

#查詢用戶
mysql> use mysql;
mysql> select host,user,plugin from user;

#設(shè)置權(quán)限與密碼 #使用mysql_native_password修改加密規(guī)則
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456'; 

#設(shè)置root密碼永不過期
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; 

#允許遠程訪問
mysql> UPDATE user SET host = '%' WHERE user = 'root';

#刷新權(quán)限
mysql> flush privileges; 

4女气、新增用戶
8.0之后的mysql不支持 授權(quán)的時候就進行用戶創(chuàng)建,所以創(chuàng)建 之后才能授權(quán)测柠;

#創(chuàng)建用戶
mysql> create user 'abcd'@'localhost' identified by '123456';
#給用戶賦權(quán)限
mysql> grant all privileges on *.* to 'abcd'@'localhost';
#新創(chuàng)建的用戶炼鞠,加密方式為:caching_sha2_password
#修改加密方式
mysql> alter user 'abcd'@'localhost' identified with mysql_native_password by '123456';
mysql> flush privileges; 

5、root用戶登錄不了怎么辦鹃愤?

mysql有一個默認用戶:debian-sys-maint

在這個文件中:/etc/mysql/debian.cnf

sudo  cat /etc/mysql/debian.cnf 
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint(這個就是默認用戶)
password = X4k1p11iFNCzUGQn (這個就是密碼)
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = X4k1p11iFNCzUGQn
socket = /var/run/mysqld/mysqld.sock

然后我們就可以用這個文件里的默認用戶登錄簇搅,并修改root密碼就可以了

mysql -udebian-sys-maint -pX4k1p11iFNCzUGQn

6、mysql常用命令

#1软吐、檢查服務(wù)狀態(tài)
systemctl status mysql.service 
#或
sudo service mysql status

#2 服務(wù)啟動
sudo service mysql start 
#停止
sudo service mysql stop
#3瘩将、進入mysql數(shù)據(jù)庫
mysql -u root -p
#4、退出mysql
mysql> quit;
#5、查看數(shù)據(jù)庫版本
mysql> status;

7姿现、查看字符集

mysql> show variables like 'character%';

修改數(shù)據(jù)庫默認字符集

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

#修改配置
[mysqld]
character-set-server=utf8mb4

[mysql] default-character-set=utf8mb4

[client] default-character-set=utf8mb4

8肠仪、卸載mysql

1、在終端中查看MySQL的依賴項

#在終端中查看MySQL的依賴項
dpkg --list|grep mysql #卸載
sudo apt-get remove mysql-common
sudo apt-get autoremove --purge mysql-server-8.0</pre>

2备典、清理殘留數(shù)據(jù)

dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P

3异旧、再次查看MySQL的剩余依賴項,繼續(xù)卸載

#再次查看MySQL的剩余依賴項:
dpkg --list|grep mysql #繼續(xù)刪除剩余依賴項:
sudo apt-get autoremove --purge mysql-apt-config

4提佣、刪除原先配置文件

sudo rm -rf /etc/mysql/ /var/lib/mysql
sudo apt autoremove
sudo apt autoreclean 
#如果提示指令有誤吮蛹,就把reclean改成clean
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市拌屏,隨后出現(xiàn)的幾起案子潮针,更是在濱河造成了極大的恐慌,老刑警劉巖倚喂,帶你破解...
    沈念sama閱讀 206,602評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件每篷,死亡現(xiàn)場離奇詭異,居然都是意外死亡端圈,警方通過查閱死者的電腦和手機焦读,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來舱权,“玉大人矗晃,你說我怎么就攤上這事⊙绫叮” “怎么了喧兄?”我有些...
    開封第一講書人閱讀 152,878評論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長啊楚。 經(jīng)常有香客問我,道長浑彰,這世上最難降的妖魔是什么恭理? 我笑而不...
    開封第一講書人閱讀 55,306評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮郭变,結(jié)果婚禮上颜价,老公的妹妹穿的比我還像新娘。我一直安慰自己诉濒,他們只是感情好周伦,可當我...
    茶點故事閱讀 64,330評論 5 373
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著未荒,像睡著了一般专挪。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,071評論 1 285
  • 那天寨腔,我揣著相機與錄音速侈,去河邊找鬼。 笑死迫卢,一個胖子當著我的面吹牛倚搬,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播乾蛤,決...
    沈念sama閱讀 38,382評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼每界,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了家卖?” 一聲冷哼從身側(cè)響起眨层,我...
    開封第一講書人閱讀 37,006評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎篡九,沒想到半個月后谐岁,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,512評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡榛臼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,965評論 2 325
  • 正文 我和宋清朗相戀三年伊佃,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片沛善。...
    茶點故事閱讀 38,094評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡航揉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出金刁,到底是詐尸還是另有隱情帅涂,我是刑警寧澤,帶...
    沈念sama閱讀 33,732評論 4 323
  • 正文 年R本政府宣布尤蛮,位于F島的核電站媳友,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏产捞。R本人自食惡果不足惜醇锚,卻給世界環(huán)境...
    茶點故事閱讀 39,283評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望坯临。 院中可真熱鬧焊唬,春花似錦、人聲如沸看靠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,286評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挟炬。三九已至鸥滨,卻和暖如春嗦哆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背爵赵。 一陣腳步聲響...
    開封第一講書人閱讀 31,512評論 1 262
  • 我被黑心中介騙來泰國打工吝秕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人空幻。 一個月前我還...
    沈念sama閱讀 45,536評論 2 354
  • 正文 我出身青樓烁峭,卻偏偏與公主長得像,于是被迫代替她去往敵國和親秕铛。 傳聞我的和親對象是個殘疾皇子约郁,可洞房花燭夜當晚...
    茶點故事閱讀 42,828評論 2 345

推薦閱讀更多精彩內(nèi)容