一、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!