一、首先清除CentOS7系统中默认的数据库mariadb,否则不能安装mysql
rpm -qa |grep mariadb |xargs yum remove -y
二、安装MySql
1、下载MySql的相关rpm包
在https://dev.mysql.com/downloads/mysql/选择为Red Hat Enterprise Linux 7/Oracle Linux 7,把os的版本选择为all,直接下载mysql-8.0.15-1.el7.x86_64.rpm-bundle.tar,所有的rpm包都在里面;
2、将下载的mysql-8.0.15-1.el7.x86_64.rpm-bundle.tar放到/usr/local/mysql目录,解压缩安装包
tar axvf mysql-8.0.15-1.el7.x86_64.rpm-bundle.tar
3、切换到下载包目录下(cd 你的下载目录),然后对每个包进行一次安装;
rpm -ivh mysql-community-common-8.0.15-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.15-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-8.0.15-1.el7.x86_64.rpm
rpm -ivh mysql-community-embedded-compat-8.0.15-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-8.0.15-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.15-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.15-1.el7.x86_64.rpm
4、修改MySql配置
vi /etc/my.cnf
修改配置如下
#datadir=/var/lib/mysql
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
5、通过以下命令,完成对 mysql 数据库的初始化和相关配置
mysqld --initialize
chown mysql:mysql /data/mysql -R
systemctl start mysqld.service
systemctl enable mysqld
三、启动MySql服务
1、启动MySql
systemctl start mysqld.service
#停止MySql
systemctl stop mysqld.service
#重启MySql
systemctl restart mysqld.service
2、设置MySql开机自启
systemctl enable mysqld
3、通过 cat /var/log/mysqld.log | grep password 命令查看数据库的密码
2019-02-16T09:46:38.945518Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ,#t#dlkOG0j0
以上密码为,#t#dlkOG0j0
4、测试MySql安装是否成功
4.1、以root用户登录MySql,执行命令
mysql -u root -p
输入以上命令回车进入,出现输入密码提示
4.2、输入刚刚查到的密码,进行数据库的登陆,复制粘贴就行,MySQL 的登陆密码也是不显示的
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 11
Server version: 8.0.15
Copyright (c) 2000, 2019, 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>
4.3、通过 ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘root’; 命令来修改密码
set global validate_password.policy = 0;
set global validate_password.length=6;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'xhw888';
4.4、通过 exit; 命令退出 MySQL,然后通过新密码再次登陆
至此,mysql8.0.15数据库就安装完成了。
四、MySql远程访问授权配置
1、以root用户登录MySql
mysql -u root -p
2、使root(也可以配置其他的用户,如hadoop)用户可以操作数据库hive中的所有表
create user 'root'@'%' identified with mysql_native_password by 'xhw888';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
附:
创建新用户: CREATE USER ‘用户名’@‘host名称’ IDENTIFIED WITH mysql_native_password BY ‘密码’;
给新用户授权:GRANT ALL PRIVILEGES ON . TO ‘用户名’@‘host名称’;
刷新权限: FLUSH PRIVILEGES;