环境:
CentOS 7,mysql5.7
摘要说明:
在CentOS 7系统下默认因musql闭源而无musql的yum源,默认存在MariaDB的yum源,但部分系统可能考虑稳定需安装mysql;本篇文章主要讲述如何安装mysql的yum安装源及安装mysql和相关配置;
步骤:
1.完全卸载mysql
先查找安装组件:
rpm -qa|grep -i mysql
再使用rpm -e根据提示依次卸载相关组件,顺序为server--》client--》community-release--》-community-libs-compat--》community-libs--》community-common:
rpm -e xxx
如果提示依赖包错误,则使用以下命令尝试:
rpm -e xxxx --nodeps
再查找之前老版本mysql的目录、并且删除老版本mysql的文件和库:
find / -name mysql
再依次删除这些目录:
rm -rf xxx
卸载后/etc/my.cnf不会删除,需要进行手工删除
rm -rf /etc/my.cnf
2.安装mysql的yum源
官网(https://dev.mysql.com/downloads/repo/yum/)现在只能找到8.0的yum源下载地址:
http://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
通过其他分享找到5.7版本的yum源下载地址:
http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
下载yum源:
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
安装yum源:
yum -y install mysql57-community-release-el7-10.noarch.rpm
2.安装mysql
安装mysql:
yum -y install mysql-community-server
若系统初始时则安装完成后就会覆盖掉之前的mariadb。
3.启动MariaDB及设置开机启动
启动mysql:
systemctl start mysqld.service
查询启动状态:
systemctl status mysqld.service
设置开机启动:
systemctl enable mysqld.service
4.初始化mysql
首先获取mysql初始密码:
[root@iZbp1dham6enej0lrs00riZ ~]# grep "password" /var/log/mysqld.log
2018-08-07T09:23:56.764450Z 1 [Note] A temporary password is generated for root@localhost: fP*g&.Uky9a9
有两种方式初始化:
a、使用mysql_secure_installation初次配置mysql:
[root@iZbp1dham6enej0lrs00riZ ~]# mysql_secure_installation
Enter password for user root: 输入默认密码
New password: 新密码
Re-enter new password: 再次输入新密码
... Failed! Error: Your password does not satisfy the current policy requirements
New password: 密码安全级别不够需包含大小写及特殊字符
Re-enter new password: 再次输入新密码
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
New password: 再次输入新密码
Re-enter new password: 再次输入新密码
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y --》是否删除匿名用户,输入y;
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y --》是否允许远程登录,输入y;
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n
--》是否移出test数据库,输入n;
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y --》是否重新加载权限表,输入y;
Success.
All done!
b、手动修改密码:
连接数据库
mysql -u root -p
输入密码后更新密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
5.配置mysql
a.设置去掉表名的大小写敏感(否则莫名其妙的表不存在):
使用vi在/etc/my.cnf的mysqld下加入:
vi /etc/my.cnf
lower_case_table_names=1
重启msyql :
systemctl restart mysqld.service
b.设置远程连接
使用grant all privileges on *.* to root@"%" identified by "new password"进行远程连接设置;
[root@iZbp1dham6enej0lrs00riZ etc]# mysql -u root -p
Enter password:
mysql> use mysql;
mysql> select host,user from user;
mysql> grant all privileges on *.* to root@"%" identified by "new password";
mysql> flush privileges;