centos 通过yum 安装 mysql 5.7

  1. 下载 mysql 源安装包
1
curl -LO http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
  1. 安装 mysql 源

    1
    sudo yum localinstall mysql57-community-release-el7-11.noarch.rpm
  2. 安装 mysql

    1
    sudo yum install mysql-community-server

一顿按 y 就可以了.

  1. 启动mysql

    1
    2
    3
    4
    5
    6
    // 安装服务
    [root@localhost share]# sudo systemctl enable mysqld
    // 启动服务
    [root@localhost share]# sudo systemctl start mysqld
    // 查看服务状态
    [root@localhost share]# sudo systemctl status mysqld
  2. 修改 root 默认密码

    1
    2
    3
    4
    MySQL 5.7 默认密码 在 /var/log/mysqld.log 文件中, 下面这个指令可以拿到默认指令.
    grep 'temporary password' /var/log/mysqld.log
    [Note] A temporary password is generated for root@localhost: **********
    登录 MySQL 并修改密码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
mysql -u root -p
Enter password:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '你自己的密码';
```

6. 修改数据库默认字符
```sql
SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

这是我的 mysql 安装以后默认的格式,一般我们实际工作中都是用 utf8mb4 格式.这里可以通过修改 /etc/my.conf 进行修改

1
2
3
4
5
6
7
8
9
10
vi /etc/my.cnf
之后 插入以下的语句 即可修改
[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld]
character_set_server=utf8mb4

  1. 允许远程访问
    默认情况下 root 账号是不允许远端访问的
    1
    2
    3
    4
    5
    6
    7
    8
    9
    mysql> mysql> select host,user  from user;
    +-----------+---------------+
    | host | user |
    +-----------+---------------+
    | localhost | mysql.session |
    | localhost | mysql.sys |
    | localhost | root |
    +-----------+---------------+
    3 rows in set (0.00 sec)

执行以下命令

1
2
3
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

执行完之后 systemctl restart mysqld 重启服务 即可.

Share Comments