1、安裝你須要準備什麼?mysql
- 能夠鏈接互聯網的一臺Macbook
- Terminal終端
brew install mysql
Tips:brew是MacOS下的包管理工具,若命令不存在,請安裝後執行。nginx
Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/services). No changes to formulae. ==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/bottles/protobuf-3.13.0.catalina.bottle.tar.gz ==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/bottles/mysql-8.0.21_1.catalina.bottle.tar.gz ==> Installing dependencies for mysql: protobuf ==> Installing mysql dependency: protobuf ==> Pouring protobuf-3.13.0.catalina.bottle.tar.gz ==> Caveats Emacs Lisp files have been installed to: /usr/local/share/emacs/site-lisp/protobuf ==> Summary ? /usr/local/Cellar/protobuf/3.13.0: 266 files, 19.8MB ==> Installing mysql ==> Pouring mysql-8.0.21_1.catalina.bottle.tar.gz ==> /usr/local/Cellar/mysql/8.0.21_1/bin/mysqld --initialize-insecure --user=yunnasheng --basedir=/usr/local/Cellar/mysql/8.0.21_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.21_1: 290 files, 291.2MB ==> `brew cleanup` has not been run in 30 days, running now... Removing: /usr/local/Cellar/gettext/0.20.1... (1,893 files, 18.4MB) Removing: /Users/yunnasheng/Library/Caches/Homebrew/gettext--0.20.1.catalina.bottle.tar.gz... (8.3MB) Removing: /usr/local/Cellar/openssl@1.1/1.1.1d... (7,983 files, 17.9MB) Removing: /usr/local/Cellar/pcre/8.43... (204 files, 5.5MB) Removing: /Users/yunnasheng/Library/Logs/Homebrew/tree... (64B) Removing: /Users/yunnasheng/Library/Logs/Homebrew/wget... (64B) Removing: /Users/yunnasheng/Library/Logs/Homebrew/libidn2... (64B) Removing: /Users/yunnasheng/Library/Logs/Homebrew/libunistring... (64B) Removing: /Users/yunnasheng/Library/Logs/Homebrew/nginx... (64B) Removing: /Users/yunnasheng/Library/Logs/Homebrew/gettext... (64B) Removing: /Users/yunnasheng/Library/Logs/Homebrew/openssl@1.1... (64B) Removing: /Users/yunnasheng/Library/Logs/Homebrew/pcre... (64B) Pruned 1 symbolic links and 3 directories from /usr/local ==> Caveats ==> protobuf Emacs Lisp files have been installed to: /usr/local/share/emacs/site-lisp/protobuf ==> mysql 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.server start
Starting MySQL . SUCCESS!
執行命令sql
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: y 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. # 設置密碼 New password: Re-enter new password: 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. # 是否要刪除匿名用戶?按 y 表示贊成,其餘任意鍵表示拒絕 Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. # 一般root用戶只從本地登陸,這樣能夠確保有人沒法猜想來自網絡的root密碼 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) : no ... skipping. 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. # 是否要刪除測試數據庫? 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!2、快速開始
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.21 Homebrew Copyright (c) 2000, 2020, 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>
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
create database local_test character set utf8mb4;
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | local_test | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
mysql>create user 'dev_user'@'%' identified by '@dev6pWd'; Query OK, 0 rows affected (0.01 sec)
若是建立用戶時出現錯誤:Your password does not satisfy the current policy requirements 請先查看密碼策略後,修改密碼再建立。安全
mysql> SHOW VARIABLES LIKE 'validate_password%'; +--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ | validate_password.check_user_name | ON | | 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 | +--------------------------------------+--------+ 7 rows in set (0.00 sec)
擴展語法bash
- 修改用戶 - bash rename user '用戶名'@'IP地址' to '新用戶名'@'IP地址';
- 修改密碼 - set password for '用戶名'@'IP地址'=Password('新密碼');
- 指定ip登陸 -( 192.168.1.開頭的local_admin用戶,密碼爲admin登陸) create user 'local_admin'@'192.168.1.%' identified by 'admin';
grant all privileges on local_test.* to 'dev_user'@'%'; Query OK, 0 rows affected (0.01 sec)
flush privileges;
# 退出MySQL mysql> exit Bye # 使用dev_user用戶登陸 mysql -u dev_user -p # 輸入密碼 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 8.0.21 Homebrew Copyright (c) 2000, 2020, 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>
# 查看擁有的數據庫 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | local_test | +--------------------+ 2 rows in set (0.00 sec) # 使用local_test庫 mysql> use local_test; Database changed mysql> # 顯示全部表 mysql> show tables; Empty set (0.00 sec)
CREATE TABLE IF NOT EXISTS `lt_dict`( `id` VARCHAR(64) NOT NULL , `code` VARCHAR(128) NOT NULL, `name` VARCHAR(128) NOT NULL, `status` VARCHAR(12) NOT NULL, PRIMARY KEY ( `id` ) )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO lt_dict (id, code, name, status) VALUES ('3c3da1d4b3b94bd9b33fb340dfdb605c', '001', 'cjack-60', '0');
mysql> select * from lt_dict; +----------------------------------+------+----------+--------+ | id | code | name | status | +----------------------------------+------+----------+--------+ | 3c3da1d4b3b94bd9b33fb340dfdb605c | 001 | cjack-60 | 0 | +----------------------------------+------+----------+--------+ 1 row in set (0.00 sec)
恭喜你!完成了MySQL的基本入門!!網絡