在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。
1 下载并安装MySQL官方的 Yum Repository
mysql官方提供的repo文件/etc/yum.repos.d/mysql-community.repo包含所有历史版本的repo地址,只需要将特定版本进行enable即可。
1.1 方式一
网址https://dev.mysql.com/downloads/repo/yum/
#wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
#yum -y install mysql57-community-release-el7-10.noarch.rpm
1.2 方式二
手动编写 /etc/yum.repos.d/mysql-community.repo文件。
For example, to install MySQL 5.7, make sure you have enabled=0 for the above subrepository entry for MySQL 8.0, and have enabled=1 for the entry for the 5.7 series
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
yum仓库设置gpgcheck=1,下载安装时会去校验GPG key,本地找不到file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql就会报此错误。解决方法:禁用gpgcheck,设置gpgcheck=0即可。
2 在线安装mysql5服务
2.1 安装
#yum -y install mysql-community-server
安装完成后就会覆盖掉之前原装的mariadb。如果是自己安装的mariadb,就需要先手动卸载mariadb。注意需要将/var/lib/mysql目录下的内容清空,否则会因为与原先的mariadb里的数据交叉在一起无法正常启动mysql服务。
/usr/bin/mysql是指:mysql的运行路径
/var/lib/mysql是指:mysql数据库文件的存放路径
/usr/lib/mysql是指:mysql的安装路径
/etc/my.cnf是指:配置文件
/etc/my.cnf.d是指:配置文件
/var/log/mysqld.log是指:日志文件
2.2 启动
#systemctl status mysqld.service
#systemctl start mysqld.service
#systemctl enable mysqld.service
#日志文件中查找密码
grep "password" /var/log/mysqld.log
#进入数据库
mysql -uroot -p
此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:密码设置必须要大小写字母数字和特殊符号,不然不能配置成功。
mysql> alter user 'root'@'localhost' identified by 'BigData@123456';
2.3 修改密码策略
如果要修改为root这样的弱密码,需要进行以下配置:查看密码策略。
mysql>show variables like '%password%';
mysql> set global validate_password_policy=LOW;
mysql> set global validate_password_length=6;
mysql> alter user ‘root’@‘localhost’ identified by ‘bigdata’;
还可以vi /etc/my.cnf
#添加validate_password_policy配置
validate_password_policy=0
#关闭密码策略
validate_password = off
2.4 开启远程访问
mysql> grant all privileges on *.* to 'root'@'%' identified by 'bigdata' with grant option;
mysql> flush privileges;
注意:ubuntu中配置允许mysql远程登陆的方式。
#cd /etc/mysql/mysql.conf.d
#vi mysqld.cnf
修改为bind-address=0.0.0.0或注释掉#bind-address=127.0.0.1,这样才允许远程连接。
#/etc/init.d/mysql restart重启使设置生效
2.5 mysql的字符编码
mysql>show variables like '%character%';
3 卸载mysql5
3.1 删除已安装的mysql包
因为这个mysql是直接用yum安装的,所以删除过程容易不少。
(1)查找下安装的包
yum list installed | grep mysql
或者
rpm -qa |grep -i mysql
(2)开始卸载
依次执行 yum remove 包名
yum remove mysql-community-common-5.7.36-1.el7.x86_64
yum remove mysql-community-client-5.7.36-1.el7.x86_64
yum remove mysql-community-server-5.7.36-1.el7.x86_64
yum remove mysql-community-libs-5.7.36-1.el7.x86_64
核查是否卸载成功
rpm -qa |grep -i mysql
3.2 删除残留文件夹
(1)查找mysql相关目录
find / -name mysql
(2)删除相关目录
rm -rf /var/lib/mysql/
rm -rf /usr/lib64/mysql/
3.3 删除配置文件
(1)删除配置文件:
rm -rf /etc/my.cnf
(2)删除/var/log/mysqld.log
如果不删除这个文件,会导致新安装的mysql无法生存新密码,导致无法登陆
rm -rf /var/log/mysqld.log
4 忘记密码的解决方案
通常在使用Mysql数据库时,如果长时间没有登陆,或者由于工作交接完成度不高,会导致数据库root登陆密码忘记,本文给大家介绍一种当忘记mysql root密码时的解决办法。
(1)停止服务
#systemctl stop mysqld.service停止服务
(2)编辑/etc/my.cnf配置文件
在[mysqld]下添加skip-grant-tables,然后保存并退出。
[mysqld]
skip-grant-tables
(3)启动mysql服务
#systemctl start mysqld.service启动服务
(4)执行mysql命令进入mysql命令行
#mysql进入命令行
update mysql.user set password=password('bigdata') where user='root';
update mysql.user set authentication_string=password("bigdata") where user="root" and host = "localhost";