Mac (M1)系统下载、安装MySQL

打印 上一主题 下一主题

主题 528|帖子 528|积分 1584

一、下载MySQL

MySQL下载地点

二、安装mysql

2.1 MySQL安装

双击安装包






到此MySQL安装完成。
2.2 MySQL配置

2.2.1 情况变量配置

1. 打开终端,输入以下下令:

  1. vim ~/.bash_profile
复制代码
2. 按i键,进入insert模式,输入以下两行代码

  1. export PATH=$PATH:/usr/local/mysql/bin  # mysql安装目录中的bin目录路径
  2. export PATH=$PATH:/usr/local/mysql/support-files
复制代码

最后按esc键,输入:wq!(注意有冒号:),然后按回车键退出
3. 回到终端,输入以下下令

  1. vim ~/.zshrc
复制代码
4. 按i键,进入insert模式,输入以下两行代码

  1. export PATH=$PATH:/usr/local/mysql/bin  # mysql安装目录中的bin目录路径
  2. export PATH=$PATH:/usr/local/mysql/support-files
  3. #mysql服务器配置文件(启动、关闭..)存放地点
复制代码

5. 在终端实行如下下令

  1. source ~/.zshrc
  2. source ~/.bash_profile
复制代码
  上述下令用于重新加载当前用户的 Zsh shell 配置文件 .zshrc和Bash shell 配置文件 .bash_profile。实行这个下令后,新的配置将立即生效,而无需重新启动终端。
  2.2.2 配置文件的创建

   ①在Windows下的Mysql中,安装目次修改my.ini文件就会对默认字符集进行配置
  ②而在Mac下,默认没有配置文件,需要自己手动创建my.cnf文件来配置
  1. 创建 my.cnf文件

在终端输入
  1. sudo vim /etc/my.cnf
复制代码
系统提示输入电脑密码,之后进入输入模式,按i键,输入以下内容(不需要做任何修改):
  1. # Example MySQL config file for medium systems.  
  2. #  
  3. # This is for a system with little memory (32M - 64M) where MySQL plays  
  4. # an important part, or systems up to 128M where MySQL is used together with  
  5. # other programs (such as a web server)  
  6. #  
  7. # MySQL programs look for option files in a set of  
  8. # locations which depend on the deployment platform.  
  9. # You can copy this option file to one of those  
  10. # locations. For information about these locations, see:  
  11. # http://dev.mysql.com/doc/mysql/en/option-files.html  
  12. #  
  13. # In this file, you can use all long options that a program supports.  
  14. # If you want to know which options a program supports, run the program  
  15. # with the "--help" option.  
  16. # The following options will be passed to all MySQL clients  
  17. [client]
  18. default-character-set=utf8
  19. #password   = your_password  
  20. port        = 3306  
  21. socket      = /tmp/mysql.sock   
  22. # Here follows entries for some specific programs  
  23. # The MySQL server  
  24. [mysqld]
  25. character-set-server=utf8  
  26. init_connect='SET NAMES utf8'
  27. port        = 3306  
  28. socket      = /tmp/mysql.sock  
  29. skip-external-locking  
  30. key_buffer_size = 16M  
  31. max_allowed_packet = 1M  
  32. table_open_cache = 64  
  33. sort_buffer_size = 512K  
  34. net_buffer_length = 8K  
  35. read_buffer_size = 256K  
  36. read_rnd_buffer_size = 512K  
  37. myisam_sort_buffer_size = 8M  
  38. # Don't listen on a TCP/IP port at all. This can be a security enhancement,  
  39. # if all processes that need to connect to mysqld run on the same host.  
  40. # All interaction with mysqld must be made via Unix sockets or named pipes.  
  41. # Note that using this option without enabling named pipes on Windows  
  42. # (via the "enable-named-pipe" option) will render mysqld useless!  
  43. #   
  44. #skip-networking  
  45. # Replication Master Server (default)  
  46. # binary logging is required for replication  
  47. log-bin=mysql-bin  
  48. # binary logging format - mixed recommended  
  49. binlog_format=mixed  
  50. # required unique id between 1 and 2^32 - 1  
  51. # defaults to 1 if master-host is not set  
  52. # but will not function as a master if omitted  
  53. server-id   = 1  
  54. # Replication Slave (comment out master section to use this)  
  55. #  
  56. # To configure this host as a replication slave, you can choose between  
  57. # two methods :  
  58. #  
  59. # 1) Use the CHANGE MASTER TO command (fully described in our manual) -  
  60. #    the syntax is:  
  61. #  
  62. #    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,  
  63. #    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;  
  64. #  
  65. #    where you replace <host>, <user>, <password> by quoted strings and  
  66. #    <port> by the master's port number (3306 by default).  
  67. #  
  68. #    Example:  
  69. #  
  70. #    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,  
  71. #    MASTER_USER='joe', MASTER_PASSWORD='secret';  
  72. #  
  73. # OR  
  74. #  
  75. # 2) Set the variables below. However, in case you choose this method, then  
  76. #    start replication for the first time (even unsuccessfully, for example  
  77. #    if you mistyped the password in master-password and the slave fails to  
  78. #    connect), the slave will create a master.info file, and any later  
  79. #    change in this file to the variables' values below will be ignored and  
  80. #    overridden by the content of the master.info file, unless you shutdown  
  81. #    the slave server, delete master.info and restart the slaver server.  
  82. #    For that reason, you may want to leave the lines below untouched  
  83. #    (commented) and instead use CHANGE MASTER TO (see above)  
  84. #  
  85. # required unique id between 2 and 2^32 - 1  
  86. # (and different from the master)  
  87. # defaults to 2 if master-host is set  
  88. # but will not function as a slave if omitted  
  89. #server-id       = 2  
  90. #  
  91. # The replication master for this slave - required  
  92. #master-host     =   <hostname>  
  93. #  
  94. # The username the slave will use for authentication when connecting  
  95. # to the master - required  
  96. #master-user     =   <username>  
  97. #  
  98. # The password the slave will authenticate with when connecting to  
  99. # the master - required  
  100. #master-password =   <password>  
  101. #  
  102. # The port the master is listening on.  
  103. # optional - defaults to 3306  
  104. #master-port     =  <port>  
  105. #  
  106. # binary logging - not required for slaves, but recommended  
  107. #log-bin=mysql-bin  
  108. # Uncomment the following if you are using InnoDB tables  
  109. #innodb_data_home_dir = /usr/local/mysql/data  
  110. #innodb_data_file_path = ibdata1:10M:autoextend  
  111. #innodb_log_group_home_dir = /usr/local/mysql/data  
  112. # You can set .._buffer_pool_size up to 50 - 80 %  
  113. # of RAM but beware of setting memory usage too high  
  114. #innodb_buffer_pool_size = 16M  
  115. #innodb_additional_mem_pool_size = 2M  
  116. # Set .._log_file_size to 25 % of buffer pool size  
  117. #innodb_log_file_size = 5M  
  118. #innodb_log_buffer_size = 8M  
  119. #innodb_flush_log_at_trx_commit = 1  
  120. #innodb_lock_wait_timeout = 50  
  121. [mysqldump]  
  122. quick  
  123. max_allowed_packet = 16M  
  124. [mysql]  
  125. no-auto-rehash  
  126. # Remove the next comment character if you are not familiar with SQL  
  127. #safe-updates  
  128. default-character-set=utf8   
  129. [myisamchk]  
  130. key_buffer_size = 20M  
  131. sort_buffer_size = 20M  
  132. read_buffer = 2M  
  133. write_buffer = 2M  
  134. [mysqlhotcopy]  
  135. interactive-timeout
复制代码
按 esc键,输入:wq!(注意有冒号:),最后按enter键退出。
2. 修改my.cnf读写权限

在终端输入如下代码:
  1. sudo chmod 664 /etc/my.cnf
复制代码
3. 在系统设置的MySQL面板中设置配置文件


   当安装MySQL后没有在控制面板指定配置文件,使用Navicat连接服务器时会报2002错误
  4. 重新启动MySQL服务



三、使用MySQL过程中遇到的BUG

3.1 无法连接MySQL服务器

使用Navicat连接MySQL报错:

3.1.1 原因分析

①MySQL 服务器未运行;
②错误的连接参数:在 Navicat 连接设置中,您大概未正确配置连接参数。请检查以下项目:


  • 主机名/地点:确保正确输入 MySQL 服务器的 IP 地点或主机名。默认情况下,‘127.0.0.1’ 表示本地主机。
  • 端口号:确保端口号与 MySQL 服务器配置的端口一致。默认情况下,MySQL 使用 3306 端口。
  • 用户名和密码:输入正确的数据库用户名和密码。确保您拥有连接到 MySQL 服务器的权限。
  • 防火墙设置:大概是因为防火墙阻止了与 MySQL 服务器的连接。请确保您的防火墙设置允许 Navicat 连接到
  • MySQL 服务器。尝试在防火墙中添加例外规则来允许进出连接。
  • MySQL 配置问题:某些 MySQL 配置大概导致无法连接。请确保 MySQL 服务器配置中的以下设置正确:

    • 绑定地点:确保 MySQL 服务器绑定到正确的 IP 地点。如果要允许本地连接,请确保绑定地点包罗 ‘127.0.0.1’ 或 ‘localhost’。
    • 授权表设置:检查 MySQL 授权表中是否正确设置了允许远程连接的权限。

3.1.2 解决方案

根据问题的大概原因,以下是解决 “Can’t connect to server on ‘127.0.0.1’” 错误的几种常见解决方案:

  • 检查 MySQL 服务器状态:确保 MySQL 服务器已启动并正在运行。您可以在下令行或系统服务管理器中检查 MySQL 服务的状态。
   如果你想测试 MySQL 服务是否正常运行,可以使用 MySQL 客户端连接到服务器,如下所示:
  1. mysql -h your_mysql_server_ip -u your_username -p
复制代码
更换 your_mysql_server_ip、your_username 为实际的 MySQL 服务器 IP 地点和用户名。这将尝试连接到 MySQL 服务器,需要输入密码来完成连接。成功连接表示 MySQL 服务正常运行。
  

  • 检查连接参数:仔细检查 Navicat 连接设置中的主机名、端口号、用户名和密码。确保这些参数与 MySQL 服务器的配置相匹配。
  • 配置防火墙:确保防火墙允许与 MySQL 服务器的连接。您可以尝试暂时关闭防火墙进行测试,如果连接成功,则说明防火墙设置大概是问题地点。然后,添加允许 Navicat 连接的入站和出站规则。
   确保防火墙允许 MySQL 连接的一般步骤,具体步骤大概因操纵系统而异
  Linux 上的防火墙配置
     

  • 检察当前防火墙规则:
  
  1. sudo iptables -L
复制代码
  或者(如果使用 firewalld):
  
  1. sudo firewall-cmd --list-all
复制代码
  

  • 允许 MySQL 服务的端口(默认为 3306):
  
  1. sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
复制代码
  或者(如果使用 firewalld):
  
  1. sudo firewall-cmd --add-port=3306/tcp --permanent
  2. sudo firewall-cmd --reload
复制代码
   macOS 上的防火墙配置
     

  • 检察当前防火墙规则:
  
  1. sudo pfctl -s rules
复制代码
  

  • 允许 MySQL 服务的端口(默认为 3306):
  
  1. sudo pfctl -e
  2. sudo echo "pass in proto tcp from any to any port 3306" > /etc/pf.anchors/com.mysql
  3. sudo pfctl -f /etc/pf.conf
复制代码
   Windows 上的防火墙配置
     

  • 打开 Windows 防火墙设置:
   

  • 在搜索框中键入 “Windows Defender Firewall with Advanced Security”,然后打开该设置。
   

  • 允许 MySQL 服务的端口(默认为 3306):
   

  • 在 “入站规则” 中,新增规则,选择 “端口”,选择 “下一步”,选择 “特定本地端口” 并输入 3306,选择 “下一步”,选择 “允许连接”,选择 “下一步”,输入规则名称,选择 “完成”。
    == 注意事项:==
     

  • 请确保在上述步骤中更换端口号(如果你的 MySQL 服务器不是默认的 3306 端口)。
  • 在修改防火墙设置后,发起使用相干的测试方法(如 ping 或 MySQL 客户端连接)来验证连接是否成功。
   请记着,允许所有泉源的连接大概存在安全风险。最好只允许来自须要主机的连接,并采取其他安全措施,如使用强密码和限定用户权限。
   

  • 检查 MySQL 配置:检察 MySQL 服务器的配置文件(通常是 my.cnf 或 my.ini)以确保绑定地点设置正确,而且授权表中允许了远程连接。
   设置绑定地点
在 MySQL 的配置文件中,bind-address 参数用于指定 MySQL 服务器监听的 IP 地点。默认情况下,MySQL 只监听本地回环地点 127.0.0.1,即仅允许本地连接。如果你希望允许远程连接,需要将 bind-address 设置为相应的 IP 地点或者 0.0.0.0,表示接受来自任何 IP 地点的连接。
以下是配置 MySQL 绑定地点的步骤:
  

  • 打开 MySQL 的配置文件,通常是 my.cnf 文件。该文件大概位于 /etc/my.cnf、/etc/mysql/my.cnf 或者 MySQL 安装目次的 my.cnf。
  • 找到并编辑以下参数:
    1. [mysqld]
    2. bind-address = 0.0.0.0
    复制代码
    或者指定特定的 IP 地点:
    1. [mysqld]
    2. bind-address = your_specific_ip
    复制代码
    如果你使用 0.0.0.0,表示 MySQL 服务器将接受来自任何 IP 地点的连接。
  • 生存并关闭配置文件。
  • 重新启动 MySQL 服务器,以使更改生效:
    1. sudo service mysql restart
    复制代码
  请注意,修改 bind-address 大概会对安全性产生影响。确保在允许远程连接之前采取得当的安全措施,例如使用强密码,限定远程用户的权限,并通过防火墙等方式控制访问。
    设置某用户允许某个IP进行远程访问
在 MySQL 中,你可以通过授权表来设置允许远程连接的权限。通常,你需要授权用户允许从特定主机或所有主机进行远程连接。以下是在 MySQL 中设置远程连接权限的一般步骤:
  

  • 使用 MySQL 下令行客户端登录到 MySQL 服务器:
    1. mysql -u your_username -p
    复制代码
    更换 your_username 为你的 MySQL 用户名,然后输入密码。
  • 为远程连接的用户授予权限:

    • 如果你要允许来自任何主机的连接,可以使用 % 作为主机名:
      1. GRANT ALL PRIVILEGES ON *.* TO 'your_remote_user'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
      复制代码
      更换 your_remote_user 和 your_password 为实际的远程用户和密码。
    • 如果你要限定连接到特定 IP 地点范围,将 % 更换为相应的 IP 地点:
      1. GRANT ALL PRIVILEGES ON *.* TO 'your_remote_user'@'your_remote_ip' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
      复制代码
      更换 your_remote_user、your_remote_ip 和 your_password 为实际的远程用户、IP 地点和密码。

  • 刷新权限:
    1. FLUSH PRIVILEGES;
    复制代码
    这将确保在运行授权下令后刷新 MySQL 的权限表。
  • 退出 MySQL 客户端:
    1. EXIT;
    复制代码
    或者使用快捷键 \q。
  请注意,上述步骤中的 WITH GRANT OPTION 表示授予用户向其他用户授予权限的能力。如果你不希望用户具有这个能力,可以省略该选项。
  最后,确保 MySQL 服务器的防火墙允许远程连接到 MySQL 的端口(默认为 3306)。在授予远程连接权限时,要特殊警惕安全性问题,并仅允许须要的主机连接。
  

  • 检查网络连接:确保您的网络连接正常工作。尝试通过 ping 下令检查与 MySQL 服务器的连通性,确保能够成功发送和吸收数据包。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

河曲智叟

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表