1 安装MySQL
(1)解压MySQL安装包:
tar -xf mysql-8.0.33-1.el7.x86_64.rpm-bundle.tar -C /opt/module/mysql
(2)卸载系统自带的mariadb:
sudo rpm -qa | grep mariadb | xargs sudorpm -e --nodeps
(3)安装MySQL依赖(一定要按顺序装):
cd /opt/module/mysql
sudo rpm -ivh mysql-community-common-8.0.33-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-libs-8.0.33-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-libs-compat-8.0.33-1.el7.x86_64.rpm
(4)安装mysql-client:
sudo rpm -ivh mysql-community-client-8.0.33-1.el7.x86_64.rpm
(5)安装mysql-server:
sudo rpm -ivh mysql-community-server-8.0.33-1.el7.x86_64.rpm
解决方法:用yum直接下载
sudo yum -y install 缺的依赖包
(6)启动MySQL:
sudo systemctl start mysqld
查看MySQL状态:
sudo systemctl status mysqld
(7)查看MySQL密码:
sudo cat /var/log/mysqld.log | grep password
控制台输出的最后显示root@localhost:....部分为初始化密码
2 配置MySQL
配置需求:
root用户 + 123456,实现在任何主机上都能登录MySQL数据库。
(1)用刚刚查到的密码进入MySQL(如果报错,给密码加单引号):
mysql -u root -p'初始密码'
报错显示权限不足,用户’root’@'localhost’不能登录到MySQL服务器:
解决在/etc/my.cnf文件中添加:
sudo vim /etc/my.cnf
然后重启MySQL服务:
sudo systemctl restart mysqld
再重新用root和初始密码登录,可以看到登录成功:
(2)更改MySQL密码策略(解决密码太简单时报错问题)此处用的是MySQL8.0.33,语法与MySQL5.7.28有区别:
mysql> set global validate_password.policy=low;
mysql> set global validate_password.length=4;
(3)设置简单好记的密码,注意MySQL8.0.33与MySQL5.7.28的语法区别:
mysql> alter user 'root'@'localhost' identified by '123456';
然后报错:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
解决:
flush privileges;
然后重新修改密码:
(4)进入MySQL库(以下为配置root用户的访问权限):
mysql> use mysql
(5)查询user表:
mysql> select user, host from user;
(6)修改user表,把Host表内容修改为%(保证root用户在任意节点都可以访问数据库):
mysql> update user sethost="%" where user="root";
(7)刷新并退出:
mysql> flush privileges;
mysql> quit;
(8)修改成功之后回到/etc/my.cnf,把刚刚添加进去的skip-grant-tables注释掉,不然后面初始化元数据库会失败:
sudo vim /etc/my.cnf
#skip-grant-tables
2 卸载MySQL
因为某种原因安装 失败,MySQL需要卸载重装:
步骤:
(1)清空原有数据:
通过/etc/my.cnf查看MySQL数据的存储位置:
sudo cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
/var/lib/mysql路径需要root权限:
切换到root
su ~ root
[root@hadoop102 ~]# cd /var/lib/mysql
[root@hadoop102 mysql]# rm -rf *
(2)卸载MySQL相关包:
查看安装过的MySQL相关包:
cd /opt/module
sudo rpm -qa | grep -i -E mysql
一键卸载命令:
rpm -qa | grep -i -E mysql\ | mariadb | xargs -n1 sudorpm -e --nodeps