在Centos7默认仓库没有Mysql下载源,大家可通过Mysql官网下载源码包自行编译或者下载Mysql官方yum仓库进行安装。这里要示范的是通过yum源进行安装Mysql5.7。
官方快速指南:https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/
Mysql repo源:https://dev.mysql.com/downloads/repo/yum/
选择对应的RPM包下载,我使用的是CentOS7,所以选择Linux7的RPM包。若使用命令行方式下载,可在shell下使用wget下载。
wget https://repo.mysql.com/mysql80-community-release-el7-2.noarch.rpm
rpm -ivh mysql80-community-release-el7-2.noarch.rpm
在使用MySQL存储库时,默认情况下会选择MySQL的最新GA版本进行安装。如果这是你想要的,你可以跳到下一步。
yum repolist all | grep mysql
输出:
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - So disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - So disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 95
mysql-connectors-community-source MySQL Connectors Community - Sou disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 84
mysql-tools-community-source MySQL Tools Community - Source disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - Sou disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - Sou disabled
mysql57-community/x86_64 MySQL 5.7 Community Server disabled
mysql57-community-source MySQL 5.7 Community Server - Sou disabled
mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 82
mysql80-community-source MySQL 8.0 Community Server - Sou disabled
因为我们这次要安装的版本是5.7,所以需要更改下选项。
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community
更改后,检查下是否是要安装的版本:
yum repolist all | grep mysql
yum -y install mysql-community-server
启动MySQL服务
systemctl start mysqld.service
检查其运行状态
systemctl status mysqld.service
输出:
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-03-12 01:10:23 CST; 21h ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3888 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 3870 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3892 (mysqld)
CGroup: /system.slice/mysqld.service
└─3892 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Mar 12 01:10:22 bogon systemd[1]: Starting MySQL Server...
Mar 12 01:10:23 bogon systemd[1]: Started MySQL Server.
开机启动MySQL服务
systemctl enable mysqld.service
如果您正在运行防火墙(firewalld),则还需要打开3306端口:
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --reload
按照官方文档描述,mysql5.7服务初始化的时候,会生成一个root'@'localhost
账号,并设置了一个临时密码,需通过获得的临时密码登录MySQL服务,并修改密码。
获取临时密码:
grep 'temporary password' /var/log/mysqld.log
通过使用生成的临时密码登录并为新帐户设置自定义密码,尽快更改根密码:
mysql -uroot -p
这里可以使用openssl生成随机密码:
openssl rand -base64 20
修改密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY '[新密码]';
MySQL的验证密码默认情况下,要求密码至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符,并且密码的总长度至少为8个字符。
这里需要注意的是,使用临时密码登录后,需修改密码后,才能进行其他操作。
关于MySQL账号密码,建议使用系统随机生成的字符串作为密码,相对比较安全,并定期修改密码。
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,建议添加一个新的帐户:
mysql> GRANT ALL PRIVILEGES ON *.* TO '[账号]'@'%' IDENTIFIED BY '[密码]' WITH GRANT OPTION;
修改/etc/my.cnf
配置文件,在[mysqld]
下添加编码配置,如下所示:
character_set_server=utf8
init_connect="SET NAMES utf8"
修改配置文件后,需重启mysql服务,使其生效
systemctl restart mysqld
如果忘记了mysql的登录密码,无法登录mysql。
可修改/etc/my.cnf
配置文件,在[mysqld]
下加入:
skip-grant-tables
重启mysql服务
systemctl restart mysqld
进入mysql
mysql -u root -p
看到输入密码,直接回车,然后输入
use mysql;
修改密码
update user set authentication_string=password('[新密码]') where User='[账号]';
修改后,退出mysql
exit;
将刚才在/etc/my.cnf
里面添加的skip-grant-tables
删除,避免出现无密码直接登录mysql控制台,删除后,记得重启mysql服务。