安装环境:Centos 7.4,mysql 5.7
1.卸载原有的mysql数据库
卸载已安装的mysql,请参考另一篇博文《CentOS中卸载MySQL》
2.下载并安装yum源
在mysql官网中查看mysql rpm 安装包 https://dev.mysql.com/downloads/repo/yum/
通过如下wget命令下载rpm安装包
[root@mair-001 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
3.根据上面下载的rpm包,本地安装yum源
[root@mair-001 ~]# yum localinstall mysql57-community-release-el7-8.noarch.rpm
4.检查mysql源是否安装成功
[root@mair-001 ~]# yum repolist enabled | grep "mysql.*-community.*";
mysql-connectors-community/x86_64 MySQL Connectors Community 51
mysql-tools-community/x86_64 MySQL Tools Community 63
mysql57-community/x86_64 MySQL 5.7 Community Server 267
如上结果,表明mysql源已经安装成功
5.修改mysql源默认安装版本
可以修改vim /etc/yum.repos.d/mysql-community.repo源,改变默认安装的mysql版本。比如要安装5.7版本,将5.7源的enabled设置为1。然后再将5.6源的enabled设置为0即可,因本次下载的是5.7版本,因为这里不需要修改,默认就是安装5.7版本
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
6.安装mysql
[root@mair-001 ~]# yum install mysql-community-server
7.启动mysql并加入开机启动
[root@mair-001 ~]# systemctl start mysqld
[root@mair-001 ~]# systemctl enable mysqld
[root@mair-001 ~]# systemctl daemon-reload
mysql安装完成之后,在/var/log/mysqld.log文件中为root用户生成了一个随机的默认密码。可以通过以下方式查看,之后可登陆MySQL进行修改
[root@mair-001 ~]# cat /var/log/mysqld.log | grep 'temporary password'
2018-05-07T15:10:19.468143Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: J(BaCuTq8hx3
使用如上临时密码登录mysql,并查看所有数据库
[root@mair-001 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.11
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases
-> ;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
show databases时会报如下错误信息,提示需要修改临时密码:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement
因此修改root密码,如下
mysql> alter user 'root'@'localhost'identified by 'Pr0d1234@!';
Query OK, 0 rows affected (0.02 sec)
再执行show databases时就成功了
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
8.创建数据库,并指定编码格式为utf8
使用create database创建数据库,并使用该数据库
mysql> create database mairuan default charset utf8;
Query OK, 1 row affected, 1 warning (0.09 sec)
mysql> use mairuan;
Database changed
9.创建用户,并为该用户分配数据权限
创建用户
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
说明:
username:你将创建的用户名
host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器
授权
GRANT privileges ON databasename.tablename TO 'username'@'host'
说明:
privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使用ALL
databasename:数据库名
tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用表示,如.*
例子:
GRANT SELECT, INSERT ON test.user TO 'pig'@'%';
GRANT ALL ON *.* TO 'pig'@'%';
注意:
用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;
10.客户端登录验证
到目前为止,mysql已经安装完成,数据库,用户都已经创建好,现使用mysql客户端heidiSQL连接数据库,发现报如下错误信息(如果是安装的MySQL8,客户端连接时会出现如下错误信息,这里安装的5.7不会出现此问题):
Authentication plugin ‘caching_sha2_password’ cannot be loaded
经查阅资料发现,mysql8.0改变了 身份验证插件,原来验证插件为:mysql_native_password,因此要么修改数据验证规则为mysql_native_password,要么修改用户登录验证规则,这里我们修改用户登录验证规则
ALTER USER 'mairuan'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #修改加密规则
ALTER USER 'mairuan'@'localhost' IDENTIFIED WITH mysql_native_password BY '{NewPassword}'; #更新密码(mysql_native_password模式)
update user set host='%' where user = 'mairuan'; #设置账号在任意ip可以使用(支持远程连接)
FLUSH PRIVILEGES; #刷新权限