獲取macos版的mysql可執行文件包
執行如下命令
cd /usr/local/src
# for Apple Chip
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.27-macos11-arm64.tar.gz
# for Intel Chip
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.27-macos11-x86_64.tar.gz
創建安裝目錄
cd /usr/local
sudo tar -zxvf src/mysql-8.0.27-macos11-arm64.tar.gz # 將mysql壓縮包解壓到 /usr/local 目錄
sudo ln -sf mysql-8.0.27-macos11-arm64 mysql
sudo chown -R dongling:staff mysql* # 將 mysql 的 owner 修改為當前用戶,此后當前用戶可以對 mysql 目錄進行讀寫操作;為了防止普通用戶讀寫,可以保持 mysql 目錄的 owner 為 root,那么后續啟動 mysql 也需要使用 root 用戶的身份來啟動。
配置環境變量
在 ~/.bashrc
中添加如下配置項
MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin:$MYSQL_HOME/support-files
mysql.server 命令工具即存在於 $MYSQL_HOME/support-files
目錄中。
然后執行
source ~/.bashrc
即可在命令行中直接使用 $MYSQL_HOME/bin
目錄下的工具了。全部工具如下
[17:52:33 dongling@noah local]180$ ll /usr/local/mysql/bin/
total 538200
drwxr-xr-x 35 dongling 1120 9 28 22:10 .
drwxr-xr-x 11 dongling 352 11 7 17:36 ..
-rwxr-xr-x 1 dongling 7017656 9 28 21:56 ibd2sdi
-rwxr-xr-x 1 dongling 6960032 9 28 21:56 innochecksum
lrwxr-xr-x 1 dongling 36 9 28 22:10 libprotobuf-lite.3.11.4.dylib -> ../lib/libprotobuf-lite.3.11.4.dylib
lrwxr-xr-x 1 dongling 31 9 28 22:10 libprotobuf.3.11.4.dylib -> ../lib/libprotobuf.3.11.4.dylib
-rwxr-xr-x 1 dongling 6881760 9 28 21:54 lz4_decompress
-rwxr-xr-x 1 dongling 6825936 9 28 21:54 my_print_defaults
-rwxr-xr-x 1 dongling 7157664 9 28 21:56 myisam_ftdump
-rwxr-xr-x 1 dongling 7309896 9 28 21:56 myisamchk
-rwxr-xr-x 1 dongling 7102568 9 28 21:56 myisamlog
-rwxr-xr-x 1 dongling 7179992 9 28 21:56 myisampack
-rwxr-xr-x 1 dongling 8508584 9 28 21:56 mysql
-rwxr-xr-x 1 dongling 5002 9 28 21:53 mysql_config
-rwxr-xr-x 1 dongling 6851744 9 28 21:56 mysql_config_editor
-rwxr-xr-x 1 dongling 8296280 9 28 21:56 mysql_migrate_keyring
-rwxr-xr-x 1 dongling 8133368 9 28 21:56 mysql_secure_installation
-rwxr-xr-x 1 dongling 6886512 9 28 21:54 mysql_ssl_rsa_setup
-rwxr-xr-x 1 dongling 6747728 9 28 21:55 mysql_tzinfo_to_sql
-rwxr-xr-x 1 dongling 8374752 9 28 21:56 mysql_upgrade
-rwxr-xr-x 1 dongling 8153624 9 28 21:56 mysqladmin
-rwxr-xr-x 1 dongling 8756296 9 28 21:57 mysqlbinlog
-rwxr-xr-x 1 dongling 8164472 9 28 21:56 mysqlcheck
-rwxr-xr-x 1 dongling 69355992 9 28 22:10 mysqld
-rwxr-xr-x 1 dongling 290130464 9 28 21:51 mysqld-debug
-rwxr-xr-x 1 dongling 27492 9 28 21:53 mysqld_multi
-rwxr-xr-x 1 dongling 29164 9 28 21:53 mysqld_safe
-rwxr-xr-x 1 dongling 8237832 9 28 21:56 mysqldump
-rwxr-xr-x 1 dongling 7669 9 28 21:53 mysqldumpslow
-rwxr-xr-x 1 dongling 8141288 9 28 21:56 mysqlimport
-rwxr-xr-x 1 dongling 9129624 9 28 21:57 mysqlpump
-rwxr-xr-x 1 dongling 8135832 9 28 21:56 mysqlshow
-rwxr-xr-x 1 dongling 8163688 9 28 21:56 mysqlslap
-rwxr-xr-x 1 dongling 7591816 9 28 21:55 perror
-rwxr-xr-x 1 dongling 6783616 9 28 21:54 zlib_decompress
啟動 mysql
此時尚且無法正常啟動mysql-server,如下
[17:54:17 dongling@noah local]188$ mysql.server start
Starting MySQL
./usr/local/mysql/bin/mysqld_safe: line 653: /usr/local/mysql/data/noah.err: No such file or directory
Logging to '/usr/local/mysql/data/noah.err'.
/usr/local/mysql/bin/mysqld_safe: line 144: /usr/local/mysql/data/noah.err: No such file or directory
/usr/local/mysql/bin/mysqld_safe: line 199: /usr/local/mysql/data/noah.err: No such file or directory
/usr/local/mysql/bin/mysqld_safe: line 916: /usr/local/mysql/data/noah.err: No such file or directory
/usr/local/mysql/bin/mysqld_safe: line 144: /usr/local/mysql/data/noah.err: No such file or directory
ERROR! The server quit without updating PID file (/usr/local/mysql/data/noah.pid).
原因是尚未在mysql的安裝目錄下創建 data 目錄。即使手動創建該目錄,依然不可行 -- 相關的數據文件並沒有在 data 目錄下創建出來。
此時,需要先執行如下命令:
[17:56:45 dongling@noah mysql]191$ mysqld --initialize-insecure
2021-11-07T09:57:58.257273Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.27-macos11-arm64/bin/mysqld (mysqld 8.0.27) initializing of server in progress as process 17225
2021-11-07T09:57:58.260194Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /usr/local/mysql-8.0.27-macos11-arm64/data/ is case insensitive
2021-11-07T09:57:58.265570Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-11-07T09:57:58.328506Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-11-07T09:57:58.697476Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2021-11-07T09:57:58.697489Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2021-11-07T09:57:58.914993Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2021-11-07T09:57:59.174234Z 0 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.27).
從輸出可以看到,mysqld 已經幫我們創建了一個 root 用戶,且該 root 用戶的 password 為空。
這里我們再看一下
--initialize-insecure
的含義
[18:06:21 dongling@noah local]201$ mysqld --help --verbose | grep -C5 "initialize-insecure"
--init-replica=name Command(s) that are executed by the replication applier
thread each time the applier threads start.
--init-slave=name This option is deprecated. Use init_replica instead.
-I, --initialize Create the default database and exit. Create a super user
with a random expired password and store it into the log.
--initialize-insecure
Create the default database and exit. Create a super user
with empty password.
--innodb Deprecated option. Provided for backward compatibility
only. The option has no effect on the server behaviour.
InnoDB is always enabled. The option will be removed in a
--
如上輸出結果,可以看到,--initialize-insecure
的作用,就是幫我們創建一個密碼為空的super user
。
此時,我們可以看到在 mysql 的安裝目錄下生成了 data 目錄,且該目錄中生成了初始化的數據庫文件
[17:59:29 dongling@noah local]194$ ll mysql/data/
total 179628
-rw-r----- 1 dongling 196608 11 7 17:57 '#ib_16384_0.dblwr'
-rw-r----- 1 dongling 8585216 11 7 17:57 '#ib_16384_1.dblwr'
drwxr-x--- 2 dongling 64 11 7 17:57 '#innodb_temp'
drwxr-x--- 24 dongling 768 11 7 17:57 .
drwxr-xr-x 12 dongling 384 11 7 17:57 ..
-rw-r----- 1 dongling 56 11 7 17:57 auto.cnf
-rw------- 1 dongling 1676 11 7 17:57 ca-key.pem
-rw-r--r-- 1 dongling 1112 11 7 17:57 ca.pem
-rw-r--r-- 1 dongling 1112 11 7 17:57 client-cert.pem
-rw------- 1 dongling 1676 11 7 17:57 client-key.pem
-rw-r----- 1 dongling 6094 11 7 17:57 ib_buffer_pool
-rw-r----- 1 dongling 50331648 11 7 17:57 ib_logfile0
-rw-r----- 1 dongling 50331648 11 7 17:57 ib_logfile1
-rw-r----- 1 dongling 12582912 11 7 17:57 ibdata1
drwxr-x--- 8 dongling 256 11 7 17:57 mysql
-rw-r----- 1 dongling 28311552 11 7 17:57 mysql.ibd
drwxr-x--- 112 dongling 3584 11 7 17:57 performance_schema
-rw------- 1 dongling 1680 11 7 17:57 private_key.pem
-rw-r--r-- 1 dongling 452 11 7 17:57 public_key.pem
-rw-r--r-- 1 dongling 1112 11 7 17:57 server-cert.pem
-rw------- 1 dongling 1676 11 7 17:57 server-key.pem
drwxr-x--- 3 dongling 96 11 7 17:57 sys
-rw-r----- 1 dongling 16777216 11 7 17:57 undo_001
-rw-r----- 1 dongling 16777216 11 7 17:57 undo_002
現在,我們就可以真正啟動 mysql 了
[18:01:30 dongling@noah local]196$ mysql.server start
Starting MySQL
.. SUCCESS!
並且可以使用空密碼登錄 root 用戶
[18:02:09 dongling@noah local]197$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27 MySQL Community Server - GPL
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 在 macbook 上安裝完畢。
給root用戶創建密碼
上面的步驟創建的root用戶,密碼為空;我們還需要給root用戶創建密碼。
以 root 用戶身份登錄mysql,執行如下命令
mysql> use mysql;
Database changed
mysql> desc user;
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(255) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
......
| authentication_string | text | YES | | NULL | |
......
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
51 rows in set (0.00 sec)
mysql>
MySQL 8.0+ 版本中,mysql.user 表格中的 password
字段已經被 authentication_string
字段代替,我們可以如下查看所有用戶的密碼
mysql> select user,host,authentication_string from user;
+------------------+-----------+------------------------------------------------------------------------+
| user | host | authentication_string |
+------------------+-----------+------------------------------------------------------------------------+
| mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.session | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.sys | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| root | localhost | NULL |
+------------------+-----------+------------------------------------------------------------------------+
此時的 root 用戶尚沒有密碼。
並且在MySQL 8.0+ 版本中,PASSWORD() 函數已經廢棄;所以,我們要使用如下的方式為 root 用戶創建密碼
mysql> ALTER USER root@localhost IDENTIFIED WITH caching_sha2_password BY 'your_password';
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT user,host,authentication_string from user;
+------------------+-----------+------------------------------------------------------------------------+
| user | host | authentication_string |
+------------------+-----------+------------------------------------------------------------------------+
| mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.session | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.sys | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| root | localhost | $A$005$iq% %H
t3
b}{X%g5i2bAFlUPIP3U6OQ5p5hn0ttZMHDnzPCrC5NfFcQaev9 |
+------------------+-----------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
此后,我們以root用戶身份登錄mysql,就需要使用密碼了。
創建普通用戶
通過代碼連接數據庫,應該使用普通用戶,而不是root用戶。所以,我們再在 mysql 中創建一個普通用戶。
mysql> CREATE USER dongling IDENTIFIED BY 'your_password';
Query OK, 0 rows affected (0.01 sec)
mysql> create database work; # 為用戶 dongling 創建一個數據倉庫 work
Query OK, 1 row affected (0.01 sec)
mysql> GRANT ALL ON work.* TO dongling; # 將數據倉庫 work 的全部權限賦予 dongling
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW GRANTS FOR dongling;
+----------------------------------------------------+
| Grants for dongling@% |
+----------------------------------------------------+
| GRANT USAGE ON *.* TO `dongling`@`%` |
| GRANT ALL PRIVILEGES ON `work`.* TO `dongling`@`%` |
+----------------------------------------------------+
2 rows in set (0.00 sec)
mysql> FLUSH PRIVILEGES; # 刷新權限
Query OK, 0 rows affected (0.00 sec)
mysql>
此后,我們就可以通過密碼以 dongling
用戶的身份登錄mysql,並可以任意操作數據倉庫 work
。
重置 root 用戶的密碼
如果我們忘記了 root
用戶的密碼,可以通過如下步驟重置 root
用戶的密碼
-
首先登錄一個 terminal 窗口,關閉 mysql-server,並以 safe 模式重新啟動
[19:40:13 dongling@noah local]25$ mysql.server stop # 關閉 mysql-server Shutting down MySQL .. SUCCESS! [19:40:24 dongling@noah local]26$ mysql.server status ERROR! MySQL is not running [19:40:23 dongling@noah etc]230$ mysqld_safe --skip-grant-tables # 以 safe 模式啟動 mysql-server,此時用戶登錄 mysql 不需要鑒權 2021-11-07T11:40:52.746034Z mysqld_safe Logging to '/usr/local/mysql/data/noah.err'. 2021-11-07T11:40:52.775183Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
該窗口中,
mysqld_safe
啟動后,一直在前台運行。 -
另起一個 terminal 窗口,執行如下命令
[19:41:35 dongling@noah local]27$ mysql -uroot #此時登錄root用戶無需密碼 Welcome to the MySQL monitor. Server version: 8.0.27 MySQL Community Server - GPL ...... mysql> UPDATE mysql.user SET authentication_string=null WHERE user='root'; #刪除 root 用戶的密碼 Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> FLUSH PRIVILEGES; # 刷新權限,這一步不可省略。即使先使用 exit 命令退出登錄,再次登錄后,依然需要執行該命令后,才能執行后續的 ALTER 命令 Query OK, 0 rows affected (0.01 sec) mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'your_password'; # 設置新密碼 Query OK, 0 rows affected (0.00 sec) mysql> exit Bye [19:42:44 dongling@noah local]28$ mysql.server stop # 此時,另一個terminal 窗口的 mysqld_safe 命令才會退出 Shutting down MySQL .. SUCCESS! [19:42:52 dongling@noah local]29$ mysql.server start # 重新啟動 mysql-server Starting MySQL . SUCCESS!
這樣,我們就為 root
用戶重置了密碼。
mysql 讀取配置文件的順序
執行如下命令
[23:28:52 dongling@noah local]137$ mysqld --verbose --help | grep -C 10 'Default options'
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.
Starts the MySQL database server.
Usage: mysqld [OPTIONS]
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
The following groups are read: mysqld server mysqld-8.0
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file,
except for login file.
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=#
Also read groups with concat(group, suffix)
可以看到
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf