天天看點

macbook 指令行安裝 mysql-8.0.27

擷取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
           

繼續閱讀