菜单

JIaLoNg
JIaLoNg
发布于 2023-06-06 / 242 阅读 / 0 评论 / 0 点赞

Ubuntu安装Mysql8.0并配置

一、 安装Mysql8.0

1、 安装Mysql

apt install mysql-server-8.0

2、 修改绑定端口

安装后会默认启动mysql服务,绑定端口为127.0.0.1,此时仅能本机访问,需要更改端口绑定为0.0.0.0

编辑文件/etc/mysql/mysql.conf.d/mysqld.cnf

修改bind-address 为0.0.0.0

修改后重启mysql服务
service mysql restart
重启成功后端口绑定为 0.0.0.0:3306

二、 修改配置

1、 本机连接数据库

#连接mysql,默认没有密码
root@hecs:~# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.31-0ubuntu0.20.04.2 (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> use 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

2、 修改用户可访问源地址(以root为例)

a、 查看用户可访问源地址,发现root用户仅能本机访问

mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

b、修改root用户任意地址可访问,刷新缓存生效

mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

3、 修改用户密码(root用户为例)

a、修改用户密码策略为mysql_native_password

mysql> ALTER USER `root`@`%` IDENTIFIED WITH mysql_native_password;
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

b、修改用户密码

mysql> ALTER USER `root`@`%` IDENTIFIED by '你的密码';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

评论