CentOS安装MySQL丨技术开发分享录

CentOS安装MySQL

{{ detail.nickname }}

转载 翻译 {{ formatTime(detail.create_time) }} 字数 {{ detail.content && detail.content.length }} 阅读 {{ detail.read_num }} {{ formatTag(v) }}

"安装环境:CentOS7 64位,MySQL5.7\n\n## 一、配置YUM源\n在MySQL官网中下载YUM源rpm安装包:[http://dev.mysql.com/downloads/repo/yum/](http://dev.mysql.com/downloads/repo/yum/)\n\n![](https://img-blog.csdn.net/20160625171308794)\n\n```shell\n# 下载mysql源安装包\nshell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm\n# 安装mysql源\nshell> yum localinstall mysql57-community-release-el7-8.noarch.rpm\n```\n\n检查mysql源是否安装成功\n\n```shell\nshell> yum repolist enabled | grep \"mysql.*-community.*\"\n```\n\n![](https://img-blog.csdn.net/20160625171753639)\n\n看到上图所示表示安装成功。\n\n可以修改 `vim /etc/yum.repos.d/mysql-community.repo` 源,改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的 `enabled=1` 改成 `enabled=0` 。然后再将5.6源的 `enabled=0` 改成 `enabled=1` 即可。改完之后的效果如下所示:\n\n![](https://img-blog.csdn.net/20160917131402303)\n\n## 二、安装MySQL\n\n```shell\nshell> yum install mysql-community-server\n```\n\n## 三、启动MySQL服务\n\n```shell\nshell> systemctl start mysqld\n```\n\n查看MySQL的启动状态\n\n```shell\nshell> systemctl status mysqld\n● mysqld.service - MySQL Server\n   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)\n   Active: active (running) since 五 2016-06-24 04:37:37 CST; 35min ago\n Main PID: 2888 (mysqld)\n   CGroup: /system.slice/mysqld.service\n           └─2888 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid\n\n6月 24 04:37:36 localhost.localdomain systemd[1]: Starting MySQL Server...\n6月 24 04:37:37 localhost.localdomain systemd[1]: Started MySQL Server.\n```\n\n或\n\n```shell\nshell> service mysqld status\n```\n\n## 四、开机启动\n\n```shell\nshell> systemctl enable mysqld\nshell> systemctl daemon-reload\n```\n\n## 五、修改root本地登录密码\n\nmysql安装完成之后,在 `/var/log/mysqld.log` 文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:\n\n```shell\nshell> grep 'temporary password' /var/log/mysqld.log\n```\n\n![image.png](/images/articles/image20201202141039.png)\n\n```shell\nshell> mysql -uroot -p\nmysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!'; \n```\n\n或者\n\n```shell\nmysql> set password for 'root'@'localhost'=password('MyNewPass4!'); \n```\n\n>[warning] 注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误,如下图所示:\n\n![](https://img-blog.csdn.net/20160625180651378)\n\n通过msyql环境变量可以查看密码策略的相关信息:\n\n```shell\nmysql> show variables like '%password%';\n```\n\n![](https://img-blog.csdn.net/20160625181000411)\n\n- validate_password_policy:密码策略,默认为MEDIUM策略\n- validate_password_dictionary_file:密码策略文件,策略为STRONG才需要\n- validate_password_length:密码最少长度\n- validate_password_mixed_case_count:大小写字符长度,至少1个\n- validate_password_number_count :数字至少1个\n- validate_password_special_char_count:特殊字符至少1个\n\n上述参数是默认策略MEDIUM的密码检查规则。\n\n共有以下几种密码策略:\n\n| 策略 | 检查规则 |\n|: --- |: --- |\n| 0 or LOW | LOW\tLength |\n| 1 or MEDIUM | Length; numeric, lowercase/uppercase, and special characters |\n| 2 or STRONG | Length; numeric, lowercase/uppercase, and special characters; dictionary file |\n\nMySQL官网密码策略详细说明:[](http://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy)\n\n**修改密码策略**\n\n在 `/etc/my.cnf` 文件添加 `validate_password_policy` 配置,指定密码策略\n\n```shell\n# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件\nvalidate_password_policy=0\n```\n\n如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:\n\n```shell\nvalidate_password = off\n```\n\n重新启动mysql服务使配置生效:\n\n```shell\nsystemctl restart mysqld\n```\n\n## 六、添加远程登录用户\n\n默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:\n\n```shell\nmysql> GRANT ALL PRIVILEGES ON *.* TO 'yangxin'@'%' IDENTIFIED BY 'Yangxin0917!' WITH GRANT OPTION;\n```\n\n## 七、配置默认编码为utf8\n\n修改 `/etc/my.cnf` 配置文件,在[mysqld]下添加编码配置,如下所示:\n\n```shell\n[mysqld]\ncharacter_set_server=utf8\ninit_connect='SET NAMES utf8'\n```\n\n重新启动mysql服务,查看数据库默认编码如下所示:\n\nmysql默认编码\n\n**默认配置文件路径:**\n\n- 配置文件:/etc/my.cnf\n- 日志文件:/var/log//var/log/mysqld.log\n- 服务启动脚本:/usr/lib/systemd/system/mysqld.service\n- socket文件:/var/run/mysqld/mysqld.pid"
PS:写作不易,如要转裁,请标明转载出处。

如果此篇对您有帮助,可小额赞助,以兹鼓励!

猜你想看