CentOS 7安装Mysql

CentOS 7安装MySql

1. 查看mysql源安装包

在MySQL官网中下载YUM源rpm安装包:[http://dev.mysql.com/downloads/repo/yum/]html

2. 下载mysql源安装包

shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

3. 安装mysql源

shell> yum localinstall mysql57-community-release-el7-9.noarch.rpm

4. 检查mysql源是否安装成功

shell> yum repolist enabled | grep "mysql.*-community.*"
[root@localhost etc]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64        MySQL Connectors Community           30
mysql-tools-community/x86_64             MySQL Tools Community                40
mysql57-community/x86_64                 MySQL 5.7 Community Server          164
[root@localhost etc]#

5. 看到上所示表示安装成功。

6. 能够修改源,改变默认安装的mysql版本。

shell> vim /etc/yum.repos.d/mysql-community.repo

7. 好比要安装5.6版本,将5.7源的enabled=1改为enabled=0。而后再将5.6源的enabled=0改为enabled=1便可。enabled=1=安装 enabled=0不安装

8. 安装MySql

shell> yum install mysql-community-server

9. 启动MySql

shell> systemctl start mysqldmysql

10. 查看MySQL的启动状态

shell> systemctl status mysqld

如下内容表示成功sql

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor 
preset: disabled)
   Active: active (running) since 日 2017-01-01 20:49:56 CST; 5s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 4866 ExecStart=/usr/sbin/mysqld --daemonize 
  --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited,       
  status=0/SUCCESS)
  Process: 4784 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, 
  status=0/SUCCESS)
 Main PID: 4869 (mysqld)
   Memory: 314.6M
   CGroup: /system.slice/mysqld.service
           └─4869 /usr/sbin/mysqld --daemonize 
           --pid-file=/var/run/mysqld/mysqld.pid
1月 01 20:49:40 localhost.localdomain systemd[1]: Starting MySQL Server...
1月 01 20:49:56 localhost.localdomain systemd[1]: Started MySQL Server.

11. 开机启动

shell> systemctl enable mysqld
 shell> systemctl daemon-reload

12. 修改root本地登陆密码

mysql安装完成以后,在/var/log/mysqld.log文件中给root生成了一个默认密码。经过下面的方式找到root默认密码,而后
登陆mysql进行修改:

shell> grep 'temporary password' /var/log/mysqld.logshell

2017-01-01T12:49:48.897043Z 1 [Note] A temporary password is generated for
root@localhost: eCMf)m6Z&?b>
eCMf)m6Z&?b> 为MySql ROOT用户密码
shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
 
或者

mysql> set password for 'root'@'localhost'=password('MyNewPass4!');
注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策
略要求密码必须包含:大小写字母、数字和特殊符号,而且长度不能少于8位。不然会提
示ERROR 1819 (HY000): Your password does not satisfy the current policy 
requirements错误,

13. 经过msyql环境变量能够查看密码策略的相关信息:

mysql> show variables like '%password%';
+---------------------------------------+--------+
| Variable_name                         | Value  |
+---------------------------------------+--------+
| default_password_lifetime             | 0      |
| disconnect_on_expired_password        | ON     |
| log_builtin_as_identified_by_password | OFF    |
| mysql_native_password_proxy_users     | OFF    |
| old_passwords                         | 0      |
| report_password                       |        |
| sha256_password_proxy_users           | OFF    |
| validate_password_check_user_name     | OFF    |
| validate_password_dictionary_file     |        |
| validate_password_length              | 8      |
| validate_password_mixed_case_count    | 1      |
| validate_password_number_count        | 1      |
| validate_password_policy              | MEDIUM |
| validate_password_special_char_count  | 1      |
+---------------------------------------+--------+
14 rows in set (0.01 sec)

validate_password_policy:密码策略,默认为MEDIUM策略
validate_password_dictionary_file:密码策略文件,策略为STRONG才须要
validate_password_length:密码最少长度
validate_password_mixed_case_count:大小写字符长度,至少1个
validate_password_number_count :数字至少1个
validate_password_special_char_count:特殊字符至少1个
上述参数是默认策略MEDIUM的密码检查规则。

共有如下几种密码策略:
策略	        检查规则
0 or LOW 	Length
1 or MEDIUM 	Length; numeric, lowercase/uppercase, and special characters
2 or STRONG 	Length; numeric, lowercase/uppercase, and special characters; 
dictionary file

MySQL官网密码策略详细说明:http://dev.mysql.com/doc/refman/5.7/en/validate-pa
ssword-options-variables.html#sysvar_validate_password_policy

14. 修改密码策略

在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略

# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2须要提供密码字典文件
validate_password_policy=0

若是不须要密码策略,添加my.cnf文件中添加以下配置禁用便可:

validate_password = off

从新启动mysql服务使配置生效:
shell>systemctl restart mysqld

15. 添加远程登陆用户

默认只容许root账户在本地登陆,若是要在其它机器上链接mysql,必须修改root容许远
程链接,或者添加一个容许远程链接的账户,为了安全起见,我添加一个新的账户:

mysql> grant all privileges on . to 'root'@'%' identified by 'MyNewPass4!' with grant option; 或者 mysql>GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY 'MyNewPass4!' WITH GRANT OPTION;数据库

16. 配置默认编码为utf8

修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,以下所示:
vi /etc/my.cnf
添加如下代码
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'

mysql> show variables like '%character%';vim

从新启动mysql服务,查看数据库默认编码以下所示:
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
相关文章
相关标签/搜索