服务器: CentOS7 64位
安装方式: YUM

一、配置YUM源

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

[root@localhost home]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

2.安装mysql源

[root@localhost home]# yum localinstall mysql57-community-release-el7-8.noarch.rpm

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

[root@localhost home]# yum repolist enabled | grep "mysql.*-community.*"

微信截图_20190424130516.png
看到上图所示表示安装成功。

可以修改vim /etc/yum.repos.d/mysql-community.repo源,改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。

二、安装MySQL

[root@localhost home]# yum install mysql-community-server

启动MySQL服务

[root@localhost home]# systemctl start mysqld

查看MySQL的启动状态

[root@localhost home]# systemctl status mysqld
状态
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-04-23 22:18:30 PDT; 3min 0s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 4729 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 4652 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 4733 (mysqld)
   Memory: 318.2M
   CGroup: /system.slice/mysqld.service
           └─4733 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Apr 23 22:18:24 localhost.localdomain systemd[1]: Starting MySQL Server...
Apr 23 22:18:30 localhost.localdomain systemd[1]: Started MySQL Server.
开机启动
[root@localhost home]# systemctl enable mysqld
[root@localhost home]# systemctl daemon-reload 

三、修改root本地登录密码

mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改,然后进入mysql
[root@localhost home]# grep 'temporary password' /var/log/mysqld.log
[root@localhost home]# mysql -u root -p
打印
2019-04-24T05:18:26.562793Z 1 [Note] A temporary password is generated for root@localhost: ioy!nkKM>7dK
[root@localhost home]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25

Copyright (c) 2000, 2019, 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> 
修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Password123456*'; 
或者
mysql> set password for 'root'@'localhost'=password('Password123456*'); 

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

四、添加远程登录用户

默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,这里添加一个新的帐户:

//root 
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Password123456*' WITH GRANT OPTION;
//yangxin
mysql> GRANT ALL PRIVILEGES ON *.* TO 'yangxin'@'%' IDENTIFIED BY 'Yangxin0917!' WITH GRANT OPTION;
// 使修改生效 
mysql>flush privileges;

五、配置默认编码为utf8

修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:

[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'

重新启动mysql服务使配置生效:

[root@localhost home]# systemctl restart mysqld
查看数据库默认编码:
[root@localhost home]# mysql -u root -p                   
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 
mysql> show variables like '%character%'
    -> ;
+--------------------------+----------------------------+
| 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.03 sec)
mysql> 
默认配置文件路径
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
Last modification:February 13th, 2020 at 03:39 pm
如果觉得我的文章对你有用,请随意赞赏