mac下使用homebrew安装mysql

本文索引:

1. homebrew是什么
2. 安装mysql
3. 配置mysql
4. 验证设置结果
5. Navicat连接报错
6. 设置允许root远程登录的权限
7. 创建mysql用户


1、 homebrew是什么

macOS (or Linux)的包管理器。

  • 安装命令
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 安装示例


    安装homebrew.png

更多信息参考homebrew官网https://brew.sh/

2、安装mysql

  • 命令
brew install mysql
  • 示例


    安装mysql.png

安装成功后会看到如下提示


mysql安装成功.png
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start

当前安装的是一个没有管理员密码的mysql数据库,当前配置只允许本地主机连接,无法使用三方客户端或者远程连接
本地连接mysql的命令为:mysql -uroot
启动mysql后台服务的命令为: brew services start mysql

3、配置mysql

➜  ~ mysql_secure_installation  //命令

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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: No
Please set the password for root here.

New password: 

Re-enter new password: 
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.

// 是否移除安装mysql时创建的匿名账户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : No 

 ... skipping.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

 // 是否不允许远程登录root用户
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y 
Success.

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.

 // 是否移除供测试的test数据库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : No

 ... skipping.
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
Success.

All done! 

4、验证设置结果

➜  ~ mysql 
ERROR 1045 (28000): Access denied for user 'apple'@'localhost' (using password: NO)
➜  ~ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.18 Homebrew

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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

5、Navicat连接报错

navicat报错.png

因为当前有很多数据库工具和链接包都不支持“caching_sha2_password”,故修改为“mysql_native_password”认证插件。

  • 在MySQL中执行命令:
// 修改密码验证插件,同时修改密码
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY '新密码';
修改密码.png
  • 再次测试,连接成功


    image.png

6、设置允许root远程登录的权限

更改数据库名为mysql表里的root用户的host就可以了。如果要允许所有ip访问,可以直接修改成“%”。


设置root登录权限.png
update user set host="%" where user="root";

7、创建mysql用户

// 1、创建用户
create user 'user1'@'%' identified with mysql_native_password by 'user112345678';
// 2、查看当前用户列表
select user,host from user;
//  3、给用户授权
grant all privileges on *.* to 'user1'@'%'; // 所有权限
grant select,insert,update, delete,create,drop,alter on *.* to 'user1'@'%';
// 4、查看用户权限
show grants for 'user1'@'%';
  • 执行示例:
mysql> create user 'user1'@'%' identified with mysql_native_password by 'user112345678';
Query OK, 0 rows affected (0.00 sec)

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

mysql> grant all privtleges on *.* to 'user1'@'%';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'privtleges on *.* to 'user1'@'%'' at line 1
mysql> grant all privileges on *.* to 'user1'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> show grants for 'user1'@'%';
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for user1@%                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `user1`@`%`                                                                                                           |
| GRANT APPLICATION_PASSWORD_ADMIN,AUDIT_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CLONE_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,INNODB_REDO_LOG_ARCHIVE,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_APPLIER,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SYSTEM_USER,SYSTEM_VARIABLES_ADMIN,TABLE_ENCRYPTION_ADMIN,XA_RECOVER_ADMIN ON *.* TO `user1`@`%` |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
  • 测试连接


    image.png

你可能感兴趣的:(mac下使用homebrew安装mysql)