MAC下安装MySQL8.0.16教训记录

问题

花了两天多时间才算解决问题!原来也在几个环境下使用过MySQL,而且也熟悉另外几个关系数据库,原以为也就是安装时间上花费一点。但是,现在总结来看,对于MAC下知识了解还是太少,导致走了不少弯路。现记录于此,有兴趣的朋友参考一下。

成都创新互联专注于尼玛网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供尼玛营销型网站建设,尼玛网站制作、尼玛网页设计、尼玛网站官网定制、微信小程序定制开发服务,打造尼玛网络公司原创品牌,更为您提供尼玛网站排名全网营销落地服务。

我使用的是从Oracle官方网站上下载目前最新的mysql-8.0.16-macos10.14-x86_64.dmg安装方式。一开始的安装过程没有问题,与其他.dmg文件的安装方式大同小异。

但是,安装结束后,在MAC终端上运行如下命令出错:

mysql -uroot -p

或者只运行mysql命令,都导致如下错误:

MAC下安装MySQL 8.0.16教训记录

这个错误可谓经典,在很多类UNIX平台上对于不同版本的MYSQL安装与初步使用中都出现过这个提示!

我在这个错误上也纠结了相当长的时间!

求解过程

在几经尝试均失败后,我注意到有网文提到分析错误跟踪日志的办法。于是,我也耐着性子跟踪分析错误日志文件mysqld.local.err。
注意,MYSQL 8.0的安全性(与在MAC平台上安装很有关系)越来越高,想分析一下其相应路径下的内容需要不断地修改权限才行。最终,观察此文件中发现有反复出现的如下提示信息:

[System] [MY-010116] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.16) starting as process 1036
2019-05-09T00:52:09.981310Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2019-05-09T00:52:09.982012Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive
2019-05-09T00:52:10.356462Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-05-09T00:52:10.356884Z 0 [ERROR] [MY-010273] [Server] Could not create unix socket lock file /tmp/mysql.sock.lock.
2019-05-09T00:52:10.356892Z 0 [ERROR] [MY-010268] [Server] Unable to setup unix socket lock file.
2019-05-09T00:52:10.357090Z 0 [ERROR] [MY-010119] [Server] Aborting
2019-05-09T00:52:11.822090Z 0 [System] [MY-010910] [Server] /usr/local/mysql/bin/mysqld: Shutdown complete (mysqld 8.0.16) MySQL Community Server - GPL.

再结合“系统偏好设置”中MYSQL图标打开时注意到的,发现MYSQL每隔几秒钟就连接与断开现象,与这里的日志记录一致了。

接下来,使用上面错误中提到的关键词“Could not create unix socket lock file /tmp/mysql.sock.lock”搜索......

因为网文中有些同学虽然解决了问题,但是由于版本不同,而且有些理解也不是很到位,导致我也跟着瞎走了不少路。

最终得到的解答是,上面错误原因在于没有对于/tmp目录的写权限所致。

于是,运行如下命令:

sudo chown -R mysql:mysql /tmp/

再次在命令行运行如下命令成功启动mysql——通过上面的“系统偏好设置”中MYSQL图标对应的面板也能观察到。

sudo mysqld_safe

有关mysqld_safe命令文件的作用在此不赘述,请自行搜索解答。

注意:上面在初步通过向导安装完MYSQL后,现在版本中并不会自动创建配置文件my.cnf,需要自己创建,其典型的位置是
/etc路径下。这个文件的内容也是我从网上搜索到的,请参考,现列举于下:

[client]
  default-character-set=utf8

#password   =root password
  port        = 3306
  socket      = /tmp/mysql.sock
  # Here follows entries for some specific programs
  # The MySQL server
  [mysqld]
  character-set-server=utf8
  init_connect='SET NAMES utf8
  port        = 3306
  socket      = /tmp/mysql.sock
  skip-external-locking
  key_buffer_size = 16M
  max_allowed_packet = 1M
table_open_cache = 64
  sort_buffer_size = 512K
  net_buffer_length = 8K
  read_buffer_size = 256K
  read_rnd_buffer_size = 512K
  myisam_sort_buffer_size = 8M
  character-set-server=utf8
  init_connect='SET NAMES utf8'
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking

  # Replication Master Server (default)
  # binary logging is required for replication
  log-bin=mysql-bin
# binary logging format - mixed recommended
    binlog_format=mixed

      # required unique id between 1 and 2^32 - 1
      # defaults to 1 if master-host is not set
      # but will not function as a master if omitted
      server-id   = 1

    # Replication Slave (comment out master section to use this)
    #
    # To configure this host as a replication slave, you can choose between
    # two methods :
    #
    # 1) Use the CHANGE MASTER TO command (fully described in our manual) -
    #    the syntax is:
    #
    #    CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,
    #    MASTER_USER=, MASTER_PASSWORD= ;
    #
    #    where you replace , ,  by quoted strings and
    #     by the master's port number (3306 by default).
    #
    #    Example:
    #
    #    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
    #    MASTER_USER='joe', MASTER_PASSWORD='secret';
    #
    # OR
#
    # 2) Set the variables below. However, in case you choose this method, then  
    #    start replication for the first time (even unsuccessfully, for example  
    #    if you mistyped the password in master-password and the slave fails to  
    #    connect), the slave will create a master.info file, and any later
    #    change in this file to the variables' values below will be ignored and  
    #    overridden by the content of the master.info file, unless you shutdown  
    #    the slave server, delete master.info and restart the slaver server.
    #    For that reason, you may want to leave the lines below untouched
    #    (commented) and instead use CHANGE MASTER TO (see above)
    #
    # required unique id between 2 and 2^32 - 1
    # (and different from the master)
    # defaults to 2 if master-host is set
    # but will not function as a slave if omitted
    #server-id       = 2
    #
    # The replication master for this slave - required
    #master-host     =   
    #
    # The username the slave will use for authentication when connecting
    # to the master - required
    #master-user     =   
        [mysqldump]
        quick
        max_allowed_packet = 16M

          [mysql]
          no-auto-rehash
          # Remove the next comment character if you are not familiar with SQL
          #safe-updates
          default-character-set=utf8

        [myisamchk]
        key_buffer_size = 20M
        sort_buffer_size = 20M
        read_buffer = 2M
        write_buffer = 2M

          [mysqlhotcopy]
          interactive-timeout

注意:上面配置文件中,在我们的兴趣范围的最关键的一行是:

socket = /tmp/mysql.sock

有关文件mysql.sock,你会在网站上搜索出很多,在此也不赘述。

总之,经过上面的操作,我们拥有了对于/tmp路径的操作权后,在正常启动mysql后,就会生成这个文件(这个文件不是事先就存在的!!!)。

另外,网文中还提到如下描述:

在配置my.cnf文件的时候,一开始我是使用Mac自带的文本编辑软件创建my.cnf文件,但是文件的编码格式是utf-8的,在MySQL读取的时候会报错,之后查找相关资料发现MySQL的配置文件需要时ASNI/ASCII格式的,所以我直接在终端中用vim命令直接创建了my.cnf文件,然后将配置写入,就可以了。
首先打开终端,cd 到 /etc路径下,然后使用命令

sudo cp ~/Desktop/my.cnf ./     (我是将my.cnf文件放在了桌面上)

将该文件拷贝到/etc路径下,然后修改文件权限

sudo chmod 664 my.cnf    

一定要修改为664,不然MySQL读取文件的时候权限会出错

说得很细致,很好。也就是说,我们应当使用命令行下的vi命令创建这个配置文件并确保其使用的是ASCII格式。默认地,使用vi创建的文件自然是纯文本格式的,符合要求。另外,你也可以很容易地使用file命令来查看一个文件的格式。

问题到此,解决了60%吧,或者说80%。至此,配置文件创建了,mysql服务能够正常启动了。

但是,运行如下命令还是出错:

mysql -uroot -p

错误如下:

ERROR 1045 (28000): Access denied for user 'root'@'localhost'``

在解决上述问题过程中,贡献最大的是文章https://www.cnblogs.com/hamsterPP/p/5274052.html 重要参考。

但是,在最后修改root用户密码时所用脚本命令过时了,不再使用UPDATE命令,而改为使用ALTER命令。对此,官网上也有明确说明。

命令如下:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

但是,在运行上述命令前一定要运行下面的刷新命令:

flush privileges;

注意,在上面重要参考文章中提到,在重置(创建)root密码过程中,需要开两个控制台窗口,切记!

最后一句心得:纸上得来终觉浅,绝知此事要躬行!


文章名称:MAC下安装MySQL8.0.16教训记录
网页链接:http://hbruida.cn/article/jcdhji.html