MySql教程

macbook 命令行安装 mysql 8.0

本文主要是介绍macbook 命令行安装 mysql 8.0,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

获取macos版的mysql可执行文件包

执行如下命令

cd /usr/local/src

wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.27-macos11-arm64.tar.gz

创建安装目录

cd /usr/local

sudo tar 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 退出当前登录,然后再重新登录,效果等同。
    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
这篇关于macbook 命令行安装 mysql 8.0的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!