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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值