一、前期准备
1.检查是否已经安装过mysql,如果已经安装,先删除;
检查是否安装过mysql:
[root@localhost /]# rpm -qa | grep mysql
如果已经安装,执行删除命令:
[root@localhost /]# rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64
再次执行查询命令,查看是否删除:
[root@localhost /]# rpm -qa | grep mysql
2.查询所有Mysql对应的文件夹,如果有,先删除;
查询mysql文件夹:
[root@localhost /]# whereis mysql
mysql: /usr/bin/mysql /usr/include/mysql
[root@localhost lib]# find / -name mysql
/data/mysql
/data/mysql/mysql
删除相关目录或文件:
[root@localhost /]# rm -rf /usr/bin/mysql /usr/include/mysql /data/mysql /data/mysql/mysql
验证是否删除完毕:
[root@localhost /]# whereis mysql
mysql:
[root@localhost /]# find / -name mysql
[root@localhost /]#
3.检查是否有mariadb(centos⼀般⾃带有);
命令查看 mariadb 的安装包:
rpm -qa | grep mariadb
卸载mariadb:
rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64 --nodeps
如果在卸载的时候出现lib被安装服务依赖,确认已经安装的服务无用之后使用下面命令进行卸载:
yum remove mysql-libs
再次查看:
rpm -qa | grep mariadb
二、安装准备
1.下载Mysql 8.0.15-1 文件;
https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.15-1.el7.x86_64.rpm-bundle.tar
2.创建⽂件夹(可⾃定义);
在/usr/local⽬录下新建⽬录mysql:
cd /usr/local
mkdir mysql
3.解压安装包;
tar -xvf mysql-8.0.15-1.el7.x86_64.rpm-bundle.tar
三、安装
先后执⾏以下命令,上⼀个完成后再执⾏下⼀个:
rpm -ivh mysql-community-common-8.0.15-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-libs-8.0.15-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-client-8.0.15-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-server-8.0.15-1.el7.x86_64.rpm --nodeps --force
初始化:
mysqld --initialize
修改mysql⽂件权限:
chown mysql:mysql /var/lib/mysql -R
启动mysql:
systemctl start mysqld
查看状态:
systemctl status mysqld.service
关闭(关闭的时候使⽤):
systemctl stop mysqld
查看密码,图中标记地方即为初始密码:
grep 'temporary password' /var/log/mysqld.log
登录后修改密码:
use user;
mysql -uroot -p初始密码;
alter user 'root'@'%' identified with mysql_native_password by 'your password';
如果这种方式无法修改,也可以进去mysql库直接用update命令修改:
update user set authentication_string = 'youer password' where user = 'root';
四、授权
1.创建⽤户以及授权(root/your password);
登陆mysql:
mysql -uroot -pyour password
创建⽤户:
create user 'username'@'%' identified with mysql_native_password by 'password';
更新⽤户:
alter user 'username'@'%' identified with mysql_native_password by 'password';
删除⽤户:
drop user 'username'@'host';
授权:
grant all privileges on *.* to 'xxx'@'%' with grant option;(所有权限)
grant privileges on databasename.tablename to 'username'@'host' [WITH GRANT OPTION];
privileges:⽤户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使⽤all;
databasename:数据库名;
tablename:表名,如果要授予该⽤户对所有数据库和表的相应操作权限则可⽤ * 表示,如*.*
with grant option 该⽤户是否可授权;
flush privileges;
demo:
##建库
create database testdb default character set utf8 collate utf8_general_ci;
##查看有哪些库
show databases;
#创建test⽤户密码为123456 允许所有主机访问。若指定IP把%替换为指定ip
create user 'test'@'%' identified with mysql_native_password by '123456';
# 授权test⽤户对库testdb的select,insert,update,delete,create,drop权限
grant select,insert,update,delete,create,drop ON testdb.* to 'test'@'%';
# 授权test⽤户所有权限
grant all on *.* to 'test'@'%';
# 将授权⽣效
flush privileges;
#撤销权限(注意⽤什么的语句授权就⽤什么样的语句收回权限,否则可能⽆法撤销权限)
revoke select,insert,update,delete,create,drop on testdb.* FROM 'test'@'%';
2.额外语句;
#查看指定⽤户权限
show grants for 'test'@'%';
#查看所有⽤户
select host,user from mysql.user;
#当前⽤户
select current_user();
#查看个⼈权限
show grants;
五、常用命令
1.登录;
#连接本机Mysql
mysql -u用户名 -p密码
#连接远程主机Mysql
mysql -h127.0.0.1 -u用户名 -p密码
#退出
exit 或者 quit
2.基础操作;
#显示数据库列表
show databases;
#进入数据库
use 库名;
#显示库中的数据表
show tables;
#显示数据表的结构
describe 表名;