1.CentOS7 添加MySQL官方源
添加MySQL的yum源
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum localinstall mysql57-community-release-el7-11.noarch.rpm
如果上述命令执行无报错的话,可以用下面的命令查看有哪些版本可选择安装
命令
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 - 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 - disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 74
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 74
mysql-tools-community-source MySQL Tools Community - Sourc 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 - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - disabled
mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 307
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server disabled
mysql80-community-source MySQL 8.0 Community Server - disabled
可以看到,除了MySQL 5.7,MySQL官方源也提供别的版本,比如说5.5、5.6、8.0,如果对别的版本有需求,安装需要的版本就行,只需用yum-config-manager启用或禁用相关的源即可,比如说禁用MySQL5.7启用MySQL8.0:
yum-config-manager --disable mysql57-community
yum-config-manager --enable mysql80-community
2.CentOS7安装MySQL 5.7/5.8/8.0
yum install mysql-community-server
MySQL Server的root密码在安装过程中被设置成了随机密码,可以通过下述命令找到MySQL的root临时密码
grep 'temporary password' /var/log/mysqld.log
2019-04-01T03:04:39.023156Z 1 [Note] A temporary password is generated for root@localhost: Qw*-)jHabxlx
root@localhost:后面便是Mysql的临时root密码
3.MySQL初始化设置
命令
yum repolist all | grep mysql
Securing the MySQL server deployment.
Enter password for user root: ##输入上面的临时root密码
The existing password for the user account root has expired. Please set a new password.
New password: ##设置新密码
Re-enter new password: ##重复密码
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : #是否更改root密码
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : ##是否移除匿名用户
... skipping.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y ##是否禁止root远程登录
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y ##是否删除测试数据库
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y ##是否立即刷新权限
Success.
All done!
4.MySQL新建数据库并指定为UTF8字符集
新建数据库的时候指定字符集,减少不必要的开发问题
create database if not exists zocodev default character set utf8 collate utf8_unicode_ci;
如果有emoji需求,也可以指定为utf8mb4字符集
create database if not exists zocodev default character set utf8mb4 collate utf8mb4_unicode_ci;
5.MySQL新建用户并赋予数据库操作权限
出于安全,最好不要使用root账户来操作自定义的数据库,一个用户指定一个数据库比较合适。比如说用来建网站,可以一个WordPress网站新建一个数据库及对应的数据库用户。
新建MySQL用户并设置密码
create user zocodev_user@'localhost' IDENTIFIED BY 'ABCabc123@@';
如果需要允许新建的MySQL用户可以远程登录,可以把上面的localhost替换成%,也就是
create user zocodev_user@'%' IDENTIFIED BY 'ABCabc123@@';
需要注意的是,指定密码也需要符合上述说的validate_password插件的要求,否则会报如下错误
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
然后赋予新建的MySQL用户指定数据库的操作权限
grant all privileges on zocodev.* to zocodev_user@localhost identified by 'ABCabc123@@';
赋予权限之后,需要刷新一下权限,当然,重启一下MySQL Server也可以
flush privileges;
如果没有意外,就能用上面新建的用户及对应的密码登录MySQL及对指定的数据库进行操作了,命令如下
mysql -u zocodev_user -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| zocodev |
+--------------------+
2 rows in set (0.00 sec)
mysql>
6.CentOS7系统开启MySQL远程访问
因为我们的MySQL不一定是安装在本地,可能是内网或者别的机器上,有的时候就有了远程访问需求,下面是配置CentOS7系统下开启MySQL远程访问的步骤。
要开启CentOS7下的MySQL远程访问,需要满足如下三个要求 1、MySQL Server监听的IP可以被外界访问,比如说:192.168.1.x或者别的IP地址。 2、用于远程登录的用户需要指定允许在别的IP地址登录。新建用户的时候指定允许登录地址为%,SQL语句参考上面新建用户部分。 3、开放了对应的端口。
在MySQL配置文件的[mysqld]添加一行,指定MySQL监听IP
vim /etc/my.cnf
[mysqld]
bind-address=0.0.0.0
添加了之后,还需要重启MySQL
systemctl restart mysqld
如果开启了防火墙的话,还需要开放MySQL监听端口
firewall-cmd --permanent --add-port=3306/tcp
firewall-cmd --reload
如果上面的命令都没问题,到这一步可以使用如下命令从本地连接远程的数据库了
mysql -u username -h 1.2.3.4 -P 3306 -p
注意
从5.6更新到8.0必须由5.7过渡,启动时如有以下报错 15577618690130.jpg

删除Mysql剩余的文件
rm -rf /var/lib/mysql/*
重新启动
systemctl start mysqld.service
博客
个人博客文章地址 CentOS7 安装配置MySQL