Ubuntu 安装mysql

一、安装mysql

sudo apt install mysql-server

sudo apt install mysql-client

sudo apt-get install libmysqlclient-dev

二、安装tools工具

sudo apt install net-tools 

三、查看socket处于 listen 状态则表示安装成功。

kuaibang@face:/etc/mysql$ sudo netstat -antpl | grep mysql
tcp        0      0 127.0.0.1:33060         0.0.0.0:*               LISTEN      131067/mysqld       
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      131067/mysqld 

四、登录mysql,使用自带用户名和密码登录

kuaibang@face:$ sudo cat /etc/mysql/debian.cnf 
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint  >>> 用户名
password = lncvxxxxxxxx2Zrd  >>> 密码
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = lncvPshdcrgR2Zrd
socket   = /var/run/mysqld/mysqld.sock

登录mysql:

sudo mysql -u debian-sys-maint -p

kuaibang@face:/etc/mysql$ sudo mysql -u debian-sys-maint -p
Enter password:       >>> 输入密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 8.0.28-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2022, 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> 

说明:

-u 表示选择登陆的用户名, -p 表示登陆的用户密码,输入密码登录到mysql

注:安装mysql,root用户密码为空,以下为修改密码步骤!

五、修改mysql密码

MySql 从8.0开始修改密码有了变化,在user表加了字段authentication_string,修改密码前先检查authentication_string是否为空。

mysql 5.7.9以后废弃了password字段和password()函数;authentication_string:字段表示用户密码,而authentication_string字段下只能是mysql加密后的41位字符串密码。所以需要用一下方式来修改root密码:

格式:ALTER user '用户名'@'主机' IDENTIFIED WITH mysql_native_password BY 'new password'; 

mysql> use mysql;    >>> 进入mysql数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>  select host, user, authentication_string, plugin from user;    >>> 查看主机,用户名,密码,认证插件类型
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host      | user             | authentication_string                                                  | plugin                |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| %         | root             | *832EB84CB764129D05D498ED9CA7E5CE9B8F83EB                              | mysql_native_password |
| localhost | debian-sys-maint | $A$005$Ay'r}*2\},kU|L-N^YFLdAR2ZjVyX.9DKmReaj6EhJ6DrgYlA3EvX5rUJlt9 | caching_sha2_password |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
5 rows in set (0.00 sec)

mysql> update user set authentication_string='' where user='root';    >>> 更新root用户密码为空
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql>  select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host      | user             | authentication_string                                                  | plugin                |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| %         | root             |                                                                        | mysql_native_password |
| localhost | debian-sys-maint | $A$005$Ay'r}*2\},kU|L-N^YFLdAR2ZjVyX.9DKmReaj6EhJ6DrgYlA3EvX5rUJlt9 | caching_sha2_password |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
5 rows in set (0.00 sec)

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'xxx';    >>> 重新设置root 密码为xxx
Query OK, 0 rows affected (0.00 sec)

mysql>  select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host      | user             | authentication_string                                                  | plugin                |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| %         | root             | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257                              | mysql_native_password |
| localhost | debian-sys-maint | $A$005$Ay'r}*2\},kU|L-N^YFLdAR2ZjVyX.9DKmReaj6EhJ6DrgYlA3EvX5rUJlt9 | caching_sha2_password |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
5 rows in set (0.00 sec)

mysql>  flush privileges;      >>> 刷新权限
Query OK, 0 rows affected (0.01 sec)

mysql> exit      >>> 退出登录
Bye

kuaibang@face:/etc/mysql$ sudo systemctl restart mysql    >>> 重启数据库
kuaibang@face:/etc/mysql$ sudo mysql -u root -p        >>> 登录mysql,验证修改的密码是否生效
Enter password:    >>> 输入旧密码
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)    >> 登录使用旧密码,报错
kuaibang@face:/etc/mysql$ sudo mysql -u root -p  >>> 再次使用新密码登录
Enter password:   >>> 输入修改后新密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.28-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2022, 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>   >>> 登录成功!

  

 至此,mysql密码修改完成,可以正常使用!

 

posted @ 2022-03-03 11:07  西瓜君~  阅读(142)  评论(0编辑  收藏  举报