Homebrew安装mysql 5.7

安装Homebrew

https://brew.sh/

image.png

brew -v

# 查看 homebrew 是否可用
brew doctor

安装mysql 5.7

# 搜索 mysql 版本
brew search mysql

# 安装 5.7
brew install [email protected]

# 配置环境变量,如下图红框1
echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

# 使配置生效
source ~/.zshrc

# 启动 mysql 服务
mysql.server start

Homebrew安装mysql 5.7_第1张图片

mysql 设置密码

mysql安装完是没有配置密码的,MySQL 被配置为默认只允许来自 localhost 的连接

# 输入安装后,提示的修改密码的命令
mysql_secure_installation


# 无法设置简单密码,增加了密码强度验证插件validate_password
 ... Failed! Error: Your password does not satisfy the current policy requirements



# 查看 msyql密码相关的几个全局参数
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+


# 修改mysql参数配置
mysql> set global validate_password_policy=0;

mysql> set global validate_password_mixed_case_count=0;

mysql> set global validate_password_number_count=0;

mysql> set global validate_password_special_char_count=0;

mysql> set global validate_password_length=3;


# 查看 是否修改成功
mysql> SHOW VARIABLES LIKE 'validate_password%';


# 修改密码
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');

mysql无法远程连接

数据库客户端、命令行可以成功连接本地数据库。
idea SpringBoot项目、其他同学电脑连接我的本地数据库失败

  • Access denied for user ‘root’@‘localhost’(using password: YES)
  • 拒绝连接
  • ERROR 1130 (HY000): Host '10.50.122.22' is not allowed to connect to this MySQL server
#  开启mysql远程访问权限


mysql> use mysql;

mysql> select user, host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | localhost         |
| mysql.session | localhost |
| mysql.sys     | localhost |
+---------------+-----------+

#  %代表的是任意机器都能通过root来远程链接到服务器

mysql> update user set host = '%' where user = 'root';
## 或者
## 赋予任何主机访问权限:
## mysql 8.0 版本前
GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
## mysql 8.0版本后:
create user root@'%' identified by 'root';
grant all privileges on *.* to 'root'@'%';


#  更新权限命令,刷新权限表使修改生效
mysql> flush privileges;

# 查看能否远程连接数据库
mysql -uroot -p -h

# 如果不能,继续下面的操作

SpringBoot项目中的配置:用户名、密码、url等都是正确的,而且其他电脑也无法访问我电脑的数据库,估计是mysql绑定的ip有问题
因为是通过homebrew安装的mysql,所以查看brew info mysql
MySQL is configured to only allow connections from localhost by default

  • 通过localhost连接本地数据库可以访问,通过本机ip访问本地数据库连接失败。
    报错信息:image.png
    image.png

    # 查看网络连接是否畅通
    ping  指定ip
    
    # 查看数据库是否连接成功
    mysql -h指定ip -u用户名 -p
    
    # 如果可以ping通,但是mysql连接不上,可能是网络组的问题,端口没有放开
    
    # 查找配置文件
     mysql --help | grep cnf
    
    # 用 Homebrew 安装 mysql,默认配置在/usr/local/etc/my.cnf;其他目录不存在my.cnf
    
    vim  /usr/local/etc/my.cnf
    
    # bind-addres的配置绑定了本地IP,所以远程无法连接
    
    # 修改为:
    bind-address = 0.0.0.0
    
    # 重启 mysql
    brew services restart mysql
    或者 mysql.server restart
    
    # 自此,数据库远程访问成功
    mysql -uroot -p -h
    

    mysql 官方文档, bind-address 的配置是这样描述的:

If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces.

If the server was started with —bind-address=127.0.0.1, it will listen for TCP/IP connections only locally on the loopback interface and will not accept remote connections.

你可能感兴趣的:(mysql)