1、先检查系统是否装有mysql
[root@localhost ~]# rpm -qa | grep mysql
[root@localhost ~]#(返回空值,说明没有安装)
2、下载mysql的repo源
[root@localhost ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
3、安装mysql-community-release-el7-5.noarch.rpm包
[root@localhost ~]# sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
安装这个包后,会获得两个mysql的yum repo源:
/etc/yum.repos.d/mysql-community.repo,
/etc/yum.repos.d/mysql-community-source.repo。
可以输入命令查看:
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
mysql-community-source.repo mysql-community.repo
4、安装mysql
[root@localhost yum.repos.d]# sudo yum install mysql-server
Is this ok [y/d/N]: y(遇到选项输入y)
安装完成后,没有密码,需要重置密码
安装后再次查看mysql
[root@localhost yum.repos.d]# rpm -qa | grep mysql
mysql-community-client-5.6.41-2.el7.x86_64
mysql-community-common-5.6.41-2.el7.x86_64
mysql-community-release-el7-5.noarch
mysql-community-libs-5.6.41-2.el7.x86_64
mysql-community-server-5.6.41-2.el7.x86_64
5、重置密码
重置密码前,首先要登录
[root@localhost yum.repos.d]# mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
出现此错误:原因是/var/lib/mysql的访问权限问题。
下面的命令把/var/lib/mysql的拥有者改为当前用户:
[root@localhost /]# sudo chown -R openscanner:openscanner /var/lib/mysql
chown: 无效的用户: "openscanner:openscanner"
更换命令,并用 ll 查看目录权限列表
[root@localhost /]# chown root /var/lib/mysql/
重启服务
[root@localhost /]# service mysqld restart
登录重置密码
[root@localhost /]# mysql -u root
mysql> use mysql;
mysql> update user set password=password('root') where user='root';
mysql> exit;
重启mysql服务器才生效:
[root@localhost /]# service mysqld restart
加入以下命令行,为root添加远程连接的能力。链接密码为 root
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";
6、开放3306端口号(centos7)
[root@localhost /]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
[root@localhost /]# firewall-cmd --reload