CentOS 7 下 yum 安装和配置 Mysql (亲测无坑)

注意:以下安装步骤请严格确认您的Linux distribution 是否是 CentOS 7 的版本,不是相同版本谨慎操作,所有操作亲测是对的,没有坑~~
一、具体安装步骤:
1、首先查看 Linux distribution 的版本号:
[root@localhost /]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
2、去 MySQL 官网下载 yum 的 rpm 安装包
[root@localhost ~]# curl -LO http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   277  100   277    0     0     53      0  0:00:05  0:00:05 --:--:--    75
  0     0    0     0    0     0      0      0 --:--:--  0:00:13 --:--:--     0
100 25680  100 25680    0     0   1827      0  0:00:14  0:00:14 --:--:--  117k
3、把 Mysql 添加到 yum 源里
[root@localhost ~]# yum localinstall mysql57-community-release-el7-11.noarch.rpm
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Examining mysql57-community-release-el7-11.noarch.rpm: mysql57-community-release-el7-11.noarch
Marking mysql57-community-release-el7-11.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================
 Package                      Arch      Version    Repository                                   Size
=====================================================================================================
Installing:
 mysql57-community-release    noarch    el7-11     /mysql57-community-release-el7-11.noarch     31 k

Transaction Summary
=====================================================================================================
Install  1 Package

Total size: 31 k
Installed size: 31 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql57-community-release-el7-11.noarch                                           1/1
  Verifying  : mysql57-community-release-el7-11.noarch                                           1/1

Installed:
  mysql57-community-release.noarch 0:el7-11

Complete!
4、检查 yum 源是否安装成功
[root@localhost ~]# yum repolist enabled mysql57-community-release-el7-11.noarch.rpm
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
mysql-connectors-community                                                    | 2.5 kB  00:00:00
mysql-tools-community                                                         | 2.5 kB  00:00:00
mysql57-community                                                             | 2.5 kB  00:00:00
(1/3): mysql-connectors-community/x86_64/primary_db                           |  41 kB  00:00:01
(2/3): mysql-tools-community/x86_64/primary_db                                |  58 kB  00:00:03
(3/3): mysql57-community/x86_64/primary_db                                    | 177 kB  00:00:07
repolist: 0
5、开始安装 Mysql
[root@localhost ~]# yum install mysql-community-server
6、查看 Mysql 版本
[root@localhost ~]# mysql --version
mysql  Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using  EditLine wrapper
7、启动 Mysql
[root@localhost ~]# systemctl enable mysqld
[root@localhost ~]# systemctl start mysqld
8、查看 Mysql 服务状态
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2019-05-08 16:03:29 CST; 1min 7s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 18097 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 18024 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 18100 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─18100 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

May 08 16:03:24 localhost.localdomain systemd[1]: Starting MySQL Server...
May 08 16:03:29 localhost.localdomain systemd[1]: Started MySQL Server.
9、获取 Mysql root 默认密码

MySQL 5.7 启动后,在 /var/log/mysqld.log 文件中给 root 生成了一个默认密码,首先拿到默认密码:

[root@localhost /]# grep 'temporary password' /var/log/mysqld.log
2019-05-08T08:03:25.123480Z 1 [Note] A temporary password is generated for root@localhost: ****默认密码****
10、用默认密码登录进 Mysql
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26

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>
11、修改 Mysql root 用户密码

注意:MySQL 5.7 默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于 8 位。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Abb1234.';
Query OK, 0 rows affected (0.00 sec)
12、重启 Mysql 使重置的密码生效
# 退出 mysql 服务
mysql> quit
Bye
# 重启
[root@localhost ~]# systemctl restart mysqld
13、添加 Mysql 用户
mysql> SELECT user, host FROM mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+

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

mysql> GRANT ALL PRIVILEGES ON *.* TO 'seina'@'%' IDENTIFIED BY 'Seina123.' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.01 sec)
14、修改 Mysql 默认字符编码
# 查看 mysql 字符集编码
mysql> SHOW VARIABLES LIKE '%char%' ;
+--------------------------------------+----------------------------+
| Variable_name                        | Value                      |
+--------------------------------------+----------------------------+
| character_set_client                 | latin1                     |
| character_set_connection             | latin1                     |
| character_set_database               | latin1                     |
| character_set_filesystem             | binary                     |
| character_set_results                | latin1                     |
| character_set_server                 | latin1                     |
| character_set_system                 | utf8                       |
| character_sets_dir                   | /usr/share/mysql/charsets/ |
| validate_password_special_char_count | 1                          |
+--------------------------------------+----------------------------+
9 rows in set (0.01 sec)

退出 Mysql,修改配置文件my.cnf,如果找不到可以搜索 whereis my.cnf :

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# 修改 1 
[client]
default-charater-set=utf8

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

# 修改 2
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf_general_ci
# 修改之后,再次查看 mysql 字符集编码
mysql> show variables like '%char%';
+--------------------------------------+----------------------------+
| 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/ |
| validate_password_special_char_count | 1                          |
+--------------------------------------+----------------------------+
9 rows in set (0.01 sec)

最后如果要连接的话,注意关闭 firewall 防火墙,或者开放 3306 端口,不再详细说明。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值