安装
方式1
mysql 安装包安装 下载地址 dev.mysql.com/downloads/f…
安装后进入
mysql -u root -p
提示:-bash: mysql: command not found
遇上-bash: mysql: command not found的情况别着急,这个是因为/usr/local/bin目录下缺失mysql导致,只需建立软链接,即可以解决, 把mysql安装目录,比如MYSQLPATH/bin/mysql,映射到/usr/local/bin目录下:
cd /usr/local/bin
ln -fs /usr/local/mysql-8.0.11-macos10.13-x86_64/bin/mysql mysql
修改密码
在MySQL8.0.4以前,执行 SET PASSWORD=PASSWORD('修改的密码'); 即可修改密码。
如果mysql是8.0版本以上,这样默认是不行的。因为之前,MySQL的密码认证插件是“mysql_native_password”,而现在使用的是“caching_sha2_password”。
因为当前有很多数据库工具和链接包都不支持“caching_sha2_password”,为了方便,我暂时还是改回了“mysql_native_password”认证插件。
在MySQL中执行命令:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
启动时异常: 可能因为mysqld 无读写权限 /usr/local/Cellar/mysql@5.7/5.7.27_1/data ,需要给mysqld 赋权
sudu chmod -R 777 /usr/local/Cellar/mysql\@5.7/5.7.27_1/bin/mysqld
启动即可
方式2
使用brew 安装
安装brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
使用 brew 安装mysql 选择5.7 版本
brew install mysql@5.7
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/mysql@5.7-5.7.23.high_sierra.
Already downloaded: /Users/fei/Library/Caches/Homebrew/mysql@5.7-5.7.23.high_sierra.bottle.tar.gz
==> Pouring mysql@5.7-5.7.23.high_sierra.bottle.tar.gz
==> /usr/local/Cellar/mysql@5.7/5.7.23/bin/mysqld --initialize-insecure --user=fei
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
mysql -uroot
This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile
For compilers to find this software you may need to set:
LDFLAGS: -L/usr/local/opt/mysql@5.7/lib
CPPFLAGS: -I/usr/local/opt/mysql@5.7/include
To have launchd start mysql@5.7 now and restart at login:
brew services start mysql@5.7
Or, if you don't want/need a background service you can just run:
/usr/local/opt/mysql@5.7/bin/mysql.server start
按提示brew 已经给安装了一个无密码的数据库 需执行mysql_secure_installation 初始化数据库即可,创建新密码的数据信息
如果mysql 等命令不存在可以先将 对应home目录映射到环境变量当中
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile
当使用mysql_secure_installation命令时如果报以下错误
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
请按提示使用 mysql.server start 命令
然后再次调用mysql_secure_installation 即可
$ mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD PLUGIN 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 plugin?
// 密码长度y 是8位以上 n 可以是6位
Press y|Y for Yes, any other key for No: n
Please set the password for root here.
New password: // 新密码
Re-enter new password: // 确认密码
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) : y
Success.
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) : n
... 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.
// 是否删除test库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
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) : y
Success.
All done!
安装初始完成
登录mysql
mysql -u root -p
用户管理
1创建用户
select host,user,authentication_string from mysql.user;/*查询现有用户*/
create user "用户名"@"可以访问ip/host/%" identified by "密码";
drop user '用户名'@'host'; /*删除用户*/
用户授权
/*授予用户通过外网IP对于该数据库的全部权限*/
grant all privileges on `test`.* to 'test'@'%' ;
/*授予用户在本地服务器对该数据库的全部权限*/
grant all privileges on `test`.* to 'test'@'localhost';
grant select on test.* to 'user1'@'localhost'; /*给予查询权限*/
grant insert on test.* to 'user1'@'localhost'; /*添加插入权限*/
grant delete on test.* to 'user1'@'localhost'; /*添加删除权限*/
grant update on test.* to 'user1'@'localhost'; /*添加权限*/
flush privileges; /*刷新权限*/
2修改密码
方法1: 用SET PASSWORD命令
首先登录MySQL。
mysql> set password for 用户名@localhost = password('新密码');
方法2:用mysqladmin
mysqladmin -u用户名 -p旧密码 password 新密码
方法3:用UPDATE直接编辑user表
首先登录MySQL。
mysql> use mysql;
mysql> update user set password=password('123') where user='root' and host='localhost';
mysql> flush privileges;
3修改访问权限
use mysql;
update user set host = '访问地址/%/localHost' where user = '用户名';