记录mysql5.7的安装和配置。
windows环境(windows10)
安装和启动
- 从官网下载5.7的最新更新版,解压到本地目录,进入本目录。
- 生成data目录
mkdir data
- 初始化data目录:
.\bin\mysqld --initialize-insecure
,注意是两个横线。 - 添加配置文件
my.ini
,内容参照后面。 - 安装为系统服务,执行命令
.\bin\mysqld install
。 - 去本地服务开启mysql服务,或者执行命令
net start mysql
启动mysql服务。
配置用户密码
- 修改
my.ini
,在服务器配中添加一行skip-grant-tables
。 - 停止mysql服务器,
net stop mysql
。 - 启动mysql服务器,
net start mysql
。 - 连接mysql服务器,执行命令
.\bin\mysql -u root -p
,提示输入密码的地方直接按回车。 - 在mysql客户端执行命令
update mysql.user set authentication_string=PASSWORD('newpassword') where user='root';
修改root用户密码。 - 退出mysql客户端,删除刚才添加到
my.ini
中的skip-grant-tables
。 - 停止mysql服务器,
net stop mysql
。 - 启动mysql服务器,
net start mysql
。 - 验证:连接mysql服务器,执行命令
.\bin\mysql -u root -p
,不输入刚才的密码就不能登录服务器了。
添加新用户
- 连接mysql服务器,执行命令
.\bin\mysql -u root -p
,输入刚才的密码登录服务器。 - 新建数据库,执行命令
create database db1 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
。 - 添加数据库用户,执行命令
create user db1user1;
- 添加用户权限,执行命令
grant all privileges on db1.* to db1user1;
- 添加用户查看mysql.user的权限,执行命令
grant select on mysql.user to db1user1;
- 修改用户密码,执行
update mysql.user set authentication_string =password('db1user1password') where user='db1user1';
如果没有mysql.user的权限,使用navicat连接有权限的schema的时候,会提示没有访问mysql.user的权限的错误。可能是navicat内部访问了mysql.user。
centos环境(aliyun)
安装和启动
- wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
- rpm -ivh mysql57-community-release-el7-8.noarch.rpm
- yum -y install mysql-server
- 配置文件:/etc/my.cnf,内容参照后面。
- service mysqld restart
注:默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
配置用户密码
- grep “password” /var/log/mysqld.log
- mysql -u root -p 安装时生成的随机密码
- alter user ‘root’@‘localhost’ identified by ‘NewPassword’;
- 验证:连接mysql服务器,执行命令
.\bin\mysql -u root -p
,不输入刚才的密码就不能登录服务器了。
添加新用户
- 连接mysql服务器,执行命令
.\bin\mysql -u root -p
,输入刚才的密码登录服务器。 - 新建数据库,执行命令
create database db1 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
。 - 添加数据库用户,执行命令
create user db1user1 identified by 'db1user1password';
- 添加用户权限,执行命令
grant all privileges on db1.* to db1user1;
[mysqld]
validate_password=off
default_password_lifetime=0
以上