Mac使用brew安装mysql并将脚本数据导入数据库

本文介绍了如何在Mac上使用brew安装MySQL,通过命令行导入sql脚本到数据库,以及如何处理安装和使用过程中的常见问题,如设置root密码、访问权限错误等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Mac使用brew安装mysql

  • 安装mysql:brew install mysql

    brew 是Mac电脑用户用于安装、卸载、更新各种软件包的包管理工具。

    安装完成后,终端会给出一些提示及相关命令:在这里插入图片描述

  • 启动mysql:mysql.server start

    • mysql启动成功会提示 Starting MySQL … SUCCESS!。
    • 如果mysql服务没有启动,会报错:Error: Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
  • 为了提高mysql库的安全性,可执行 mysql_secure_installation来设置root用户密码。

    Securing the MySQL server deployment.
    Connecting to MySQL using a blank password.
    VALIDATE PASSWORD COMPONENT can be used to test passwords
    and improve security. It checks the strength of password
    and allows the users to set only those passwords which are
    secure enough. Would you like to setup VALIDATE PASSWORD component?
    Press y|Y for Yes, any other key for No: 是否设置验证密码?
    Please set the password for root here.
    New password: 输入root用户密码
    Re-enter new password: 再次输入root用户密码
    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL without having to have
    a user account created for them. This is intended only for
    testing, and to make the installation go a bit smoother.
    You should remove them before moving into a production
    environment.
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : 在进入生产之前,是否删除mysql默认生成的那个匿名用户?
    … skipping.
    Normally, root should only be allowed to connect from
    ‘localhost’. This ensures that someone cannot guess at
    the root password from the network.
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : 是否禁止root用户远程登录?
    … skipping.
    By default, MySQL comes with a database named ‘test’ that
    anyone can access. This is also intended only for testing,
    and should be removed before moving into a production
    environment.
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 是否删除测试数据库?
    … skipping.
    Reloading the privilege tables will ensure that all changes
    made so far will take effect immediately.
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 是否重新加载权限表以保证所有更改生效?
    … skipping.
    All done!

  • 执行mysql -uroot 连接mysql数据库。

    报错:ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: NO)。
    报错原因:需要通过密码进行连接。

  • 执行 mysql -u root -p 通过密码连接mysql数据库。

    全拼:mysql --host=localhost --user=username --password=password
    在这里插入图片描述
    输入exit可退出连接。在这里插入图片描述
    操作数据库的时候,可能会遇到的问题
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘database’ at line 1.
    原因:sql语句的语法有误。比如:
    在这里插入图片描述

二、通过命令行将sql脚本数据导入数据库

  • 创建名为 vshare 的数据库 create database vshare;
  • 查看数据库列表 show databases;
  • 使用名为 vshare 的数据库 use vshare;
  • 将脚本数据导入数据库 source /Users/***/***/v-share/server/vshare.sql (source sql脚本文件的路径)。
    在这里插入图片描述

三、Navicat Premium 可视化管理数据库

  • 连接mysql数据库
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

四、修改数据库密码

执行命令 alter user 'root'@'localhost' identified by '123456';
在这里插入图片描述

五、访问数据库报错问题

问题1

  • 报错提示:“Client does not support authentication protocol requested by server; consider upgrading MySQL client.” 【客户端不支持服务器请求的认证协议;考虑升级 MySQL 客户端。】
  • 报错原因:mysql v8 之前的加密规则是mysql_native_password,mysql v8之后的加密规则是caching_sha2_password
  • 解决方法:检查当前mysql版本及当前用户的密码验证插件,如果mysql版本是v8以上,手动修改使用密码验证插件mysql_native_password。
    在这里插入图片描述

问题2

  • 报错提示:“Access denied for user ‘root’@‘localhost’ (using password: NO)”
  • 解决方法:确保数据库连接密码的正确。
    在这里插入图片描述
    在这里插入图片描述

End!

<think>好的,我现在需要帮助用户在Mac电脑上通过Homebrew安装使用Nacos。首先,我需要回忆一下Homebrew的基本用法,以及Nacos的相关安装步骤。用户提到之前有引用关于Homebrew安装问题的情况,比如fuse在macOS再开源导致安装失败,还有创建nacos数据库的步骤,这些可能需要特别注意。 首先,我应该确认Nacos是否在Homebrew的仓库中存在。因为有时候某些软件可能在默认的仓库里,需要添加第三方仓库。比如,可能需要使用brew tap来添加某个特定的仓库。如果用户直接运行brew install nacos,可能会提示找到软件包,这时候就需要引导用户添加正确的tap源。 接下来,安装完成后,Nacos的配置和启动步骤也很重要。用户需要知道如何修改配置文件,比如连接数据库的设置。根据引用[3],用户需要创建nacos数据库运行nacos-mysql.sql脚本,这一步在安装过程中是必须的。因此,在指南中需要详细说明如何找到这个SQL文件,执行它来初始化数据库。 另外,Mac M1芯片的兼容性可能是一个问题。需要确认Nacos是否有兼容ARM架构的版本,或者是否需要通过Rosetta 2来运行。如果存在兼容性问题,可能需要额外的步骤来解决,比如使用arch命令指定架构。 启动Nacos后,用户可能需要知道如何访问管理界面,默认的端口是8848,但有时候可能会有冲突,需要检查端口是否被占用。此外,关闭Nacos的正确方式也很重要,避免直接终止进程导致数据丢失。 最后,常见问题部分需要考虑用户可能遇到的错误,比如数据库连接失败、端口占用、权限问题等,给出解决方案。例如,引用[2]中提到Homebrew支持某些软件安装,如果用户遇到类似问题,可能需要寻找替代安装方法或者手动安装。 总结一下,整个流程应该是:安装Homebrew -> 通过Homebrew安装Nacos -> 配置数据库 -> 启动Nacos -> 访问管理界面 -> 处理常见问题。每个步骤都需要详细说明,注意可能的陷阱,比如依赖项缺失或配置错误。同时,要确保所有命令和路径都正确无误,特别是针对Mac系统可能存在的路径差异(如M1芯片的Homebrew路径可能同)。</think>### Mac通过Homebrew安装使用Nacos指南 #### 一、环境准备 1. 确保已安装最新版Homebrew ```bash /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` 若为M1/M2芯片设备,需确认终端使用Rosetta模式或安装ARM原生版本[^2] #### 二、安装Nacos 1. 添加第三方仓库 ```bash brew tap nacos-inc/homebrew-nacos ``` 2. 执行安装命令 ```bash brew install nacos ``` *注:安装过程包含自动创建nacos用户和基础目录(/usr/local/var/nacos)* #### 三、数据库配置 1. 创建MySQL数据库 ```sql CREATE DATABASE nacos_config CHARACTER SET utf8mb4; ``` 2. 导入初始化SQL ```bash mysql -u root -p nacos_config < $(brew --prefix nacos)/conf/nacos-mysql.sql ``` [^3] #### 四、修改配置文件 ```bash vim /usr/local/etc/nacos/application.properties ``` 关键配置项: ```properties spring.datasource.platform=mysql db.url.0=jdbc:mysql://localhost:3306/nacos_config?characterEncoding=utf8 db.user=root db.password=your_password ``` #### 五、启动服务 1. 单机模式启动 ```bash brew services start nacos ``` 2. 验证启动状态 ```bash tail -f /usr/local/var/log/nacos/nacos.log ``` #### 六、访问控制台 浏览器访问: ```url http://localhost:8848/nacos ``` 默认账号密码均为`nacos` #### 七、常用命令 | 操作 | 命令 | |--------------|--------------------------| | 停止服务 | `brew services stop nacos` | | 重启服务 | `brew services restart nacos` | | 查看版本 | `nacos --version` |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值