使用 apt 安裝mysql 先search一下
sudo apt search mysql
得到結果
找到了這個 發現是8 那就裝吧
sudo apt-get install mysql-server
安裝后發現無法登錄
$ mysql -uroot -p Enter password: ERROR 1698 (28000): Access denied for user 'root'@'localhost'
此時可以查看 文件
$ sudo cat /etc/mysql/debian.cnf # Automatically generated for Debian scripts. DO NOT TOUCH! [client] host = localhost user = debian-sys-maint password = koJJxVpPDRINkIE6 socket = /var/run/mysqld/mysqld.sock [mysql_upgrade] host = localhost user = debian-sys-maint password = koJJxVpPDRINkIE6 socket = /var/run/mysqld/mysqld.sock
然后使用這個用戶密碼登錄
mysql -u debian-sys-maint -pkoJJxVpPDRINkIE6
發現可以登錄進來 然后選擇 mysql 數據庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.08 sec) mysql> use mysql;
改密碼 刷新權限
mysql> alter user 'root'@'localhost' identified with mysql_native_password by '新密碼'; Query OK, 0 rows affected (0.10 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
然后退出使用新密碼重新登錄看到可以登錄了
$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> exit Bye