mac下brew安裝mysql
brew info mysql
mysql: stable 8.0.23 (bottled) Open source relational database management system https://dev.mysql.com/doc/refman/8.0/en/ Conflicts with: mariadb (because mysql, mariadb, and percona install the same binaries) percona-server (because mysql, mariadb, and percona install the same binaries) Not installed From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/mysql.rb License: GPL-2.0-only ==> Dependencies Build: cmake ? Required: openssl@1.1 ?, protobuf ? ==> Caveats 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
安裝過(guò)程
brew install mysql
==> Installing mysql ==> Pouring mysql-8.0.25_1.big_sur.bottle.tar.gz ==> /usr/local/Cellar/mysql/8.0.25_1/bin/mysqld --initialize-insecure --user=jiangbin --basedir=/usr/local/Cellar/mysql/8.0.25_1 --datadir=/usr/local/var/mysql --tmpdir=/tmp ==> Caveats 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 ==> Summary ?? /usr/local/Cellar/mysql/8.0.25_1: 300 files, 293.6MB
安裝成功后啟動(dòng)mysql服務(wù)
mysql.server start
Starting MySQL . SUCCESS!
mysql.server stop
mysql.server start
mysql.server restart
修改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? // 提示是否設(shè)置密碼 Press y|Y for Yes, any other key for No: y // 提示選擇密碼強(qiáng)度等級(jí) There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 Please set the password for root here. // 按照所選的密碼強(qiáng)度要求設(shè)定密碼 New password: Re-enter new password: // 提示密碼強(qiáng)度50,不符合要求重新設(shè)置密碼 Estimated strength of the password: 50 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y 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. // 提示刪除默認(rèn)無(wú)密碼用戶(hù) Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. // 提示禁止遠(yuǎn)程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. // 提示刪除默認(rèn)自帶的test數(shù)據(jù)庫(kù) Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. // 提示是否重新加載privilege tables Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done!
登錄mysql
mysql -u root -p
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.25 Homebrew 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>
配置Mysql遠(yuǎn)程連接
一.賦予某個(gè)用戶(hù)權(quán)限
1.賦予權(quán)限格式:grant 權(quán)限 on 數(shù)據(jù)庫(kù)對(duì)象 to 用戶(hù)@IP(或者相應(yīng)正則)
注:可以賦予select,delete,update,insert,index等權(quán)限精確到某一個(gè)數(shù)據(jù)庫(kù)某一個(gè)表。
GRANT ALL PRIVILEGES ON *.* TO '用戶(hù)名'@'%' IDENTIFIED BY '密碼' WITH GRANT OPTION;
這里表示賦予該用戶(hù)所有數(shù)據(jù)庫(kù)所有表(*.*表示所有表),%表示所有IP地址。
2.刷新權(quán)限:FLUSH PRIVILEGES;
FLUSH PRIVILEGES;
3.查看權(quán)限:*
select user,host from mysql.user;
$ /usr/local/mysql/bin/mysql -u root -p
--輸入密碼
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| seckill |
| sys |
| test |
+--------------------+
6 rows in set (0.00 sec)
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
--查詢(xún)當(dāng)前數(shù)據(jù)庫(kù)相關(guān)信息
mysql> select host,user,authentication_string,plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host | user | authentication_string | plugin |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| 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 |
| localhost | root | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | mysql_native_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
4 rows in set (0.00 sec)
--將root用戶(hù)設(shè)置為所有地址可登錄,原來(lái)是localhost表示只用本機(jī)可登錄
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
--刷新權(quán)限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
--將用戶(hù)root密碼設(shè)置為永不過(guò)期
mysql> alter user 'root'@'%' identified by '12345678' password expire never;
Query OK, 0 rows affected (0.01 sec)
--將root用戶(hù)密碼加密方式改為mysql_native_password ,上面查到root用戶(hù)密碼的加密方式為caching_sha2_password
mysql> alter user 'root'@'%' identified with mysql_native_password by '12345678';
Query OK, 0 rows affected (0.00 sec)
--刷新權(quán)限,在別的機(jī)器上即可登錄
mysql> flush privileges;
MAC 通過(guò) brew 安裝Mysql 無(wú)法使用外網(wǎng)訪問(wèn)
- 打開(kāi)/usr/local/etc/my.cnf文件
- 將bind-address = 127.0.0.1修改為# bind-address = 127.0.0.1
- 保存并重啟mysql