centos7默认支持mariadb,不支持mysql,mariadb是mysql一个开源分支。
1、查看是否已安装,执行命令:
rpm -qa | grep mariadb
输出结果:
[root@centos7 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
以上列出所有被安装的mariadb rpm 包
2、卸载mariadb,否则安装mysql会出现冲突n执行命令
rpm -e --nodeps 包名称
包名称:mariadb-libs-5.5.56-2.el7.x86_64
3、添加官方的yum源
以centos7安装mysql5.7为例。
创建并编辑mysql-community.repo文件
vi /etc/yum.repos.d/mysql-community.repo
将以下内容粘贴进去并保存
[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
注意:gpgcheck是GPG加密校验,官方文档中,值为1,但check会报错误,所以这里改为0跳过检查,对安装无影响。
PS:其他centos版本安装其他版本的mysql只需要改为对应的baseurl即可:
centos7安装mysql5.7:baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
centos6安装mysql5.6:baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
centos6安装mysql5.7:baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
4、安装mysql,执行命令:
yum install mysql-community-server
5、启动mysql,执行指令:
systemctl start mysqld
6、查看mysql状态,执行指令:
systemctl status mysqld
结果:
[root@centos7 yum.repos.d]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2018-08-23 19:54:41 CST; 4s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 8697 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 8624 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 8700 (mysqld)
CGroup: /system.slice/mysqld.service
└─8700 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
7、设置开机启动
systemctl enable mysqld
systemctl daemon-reload
vi /etc/my.cnf中添加skip-grant-tables
systemctl restart mysqld
mysql
use mysql;
update user set authentication_string=password('Hxw@1234') where user='root' and host='localhost';
flush privileges;
8、修改mysql 的root密码
mysqladmin -u root -p password 你的新密码
要求输入初始密码,mysql5.7的初始密码是随机生成的,如:hgx%tymtG3So,放在了 /var/log/mysqld.log。使用命令 grep 'temporary password' /var/log/mysqld.log 读出来即可,如下:
[root@centos7 yum.repos.d]# grep 'emporary passwo' /var/log/mysqld.log
2018-08-23T11:54:37.871131Z 1 [Note] A temporary password is generated for root@localhost: hgx%tymtG3So
注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则,会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误。
9、添加远程登录用户
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,可添加一个新的帐户:
mysql -uroot -p
use mysql #使用mysql表
mysql> GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'young'@'%' IDENTIFIED BY 'younglin' WITH GRANT OPTION;
说明:
1、如果允许root账号远程访问,将用户名写成root即可。
2、"%"表示任何主机都可以远程登录到该服务器上访问。如果要限制只有某台机器可以访问,将其换成相应的IP即可,如:
GRANT ALL PRIVILEGES ON *.* TO '用户名'@"172.168.193.25" IDENTIFIED BY '密码' WITH GRANT OPTION;
10、防火墙添加3306端口权限
firewall-cmd --zone=public --add-port=3306/tcp --permanent
systemctl restart firewalld #重启防火墙