CentOS安装MySQL

作者 拓荒 日期 2017-06-14

1. 下载mysql的repo源

# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
--2017-06-14 11:32:25-- http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm [following]
--2017-06-14 11:32:26-- https://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://repo.mysql.com//mysql-community-release-el7-5.noarch.rpm [following]
--2017-06-14 11:32:27-- https://repo.mysql.com//mysql-community-release-el7-5.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.211.97.88
Connecting to repo.mysql.com (repo.mysql.com)|23.211.97.88|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6140 (6.0K) [application/x-redhat-package-manager]
Saving to: ‘mysql-community-release-el7-5.noarch.rpm’

100%[===========================================================>] 6,140 --.-K/s in 0s

2017-06-14 11:32:27 (269 MB/s) - ‘mysql-community-release-el7-5.noarch.rpm’ saved [6140/6140]

2. 安装mysql-community-release-el7-5.noarch.rpm包

# rpm -ivh mysql-community-release-el7-5.noarch.rpm
Preparing... ################################# [100%]
package mysql-community-release-el7-5.noarch is already installed

3. 安装mysql

# yum install mysql-community-server
Loaded plugins: fastestmirror
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* remi-safe: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.6.36-2.el7 will be installed
--> Processing Dependency: mysql-community-client(x86-64) >= 5.6.10 for package: mysql-community-server-5.6.36-2.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.6.36-2.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.6.10 for package: mysql-community-client-5.6.36-2.el7.x86_64
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.6.36-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================
Package Arch Version Repository Size
=====================================================================================================
Installing:
mysql-community-server x86_64 5.6.36-2.el7 mysql56-community 59 M
Installing for dependencies:
mysql-community-client x86_64 5.6.36-2.el7 mysql56-community 19 M
mysql-community-libs x86_64 5.6.36-2.el7 mysql56-community 2.0 M

Transaction Summary
=====================================================================================================
Install 1 Package (+2 Dependent packages)

Total download size: 81 M
Installed size: 349 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): mysql-community-libs-5.6.36-2.el7.x86_64.rpm | 2.0 MB 00:00:00
(2/3): mysql-community-client-5.6.36-2.el7.x86_64.rpm | 19 MB 00:00:01
(3/3): mysql-community-server-5.6.36-2.el7.x86_64.rpm | 59 MB 00:00:04
-----------------------------------------------------------------------------------------------------
Total 17 MB/s | 81 MB 00:00:04
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql-community-libs-5.6.36-2.el7.x86_64 1/3
Installing : mysql-community-client-5.6.36-2.el7.x86_64 2/3
Installing : mysql-community-server-5.6.36-2.el7.x86_64 3/3
Verifying : mysql-community-server-5.6.36-2.el7.x86_64 1/3
Verifying : mysql-community-libs-5.6.36-2.el7.x86_64 2/3
Verifying : mysql-community-client-5.6.36-2.el7.x86_64 3/3

Installed:
mysql-community-server.x86_64 0:5.6.36-2.el7

Dependency Installed:
mysql-community-client.x86_64 0:5.6.36-2.el7 mysql-community-libs.x86_64 0:5.6.36-2.el7

Complete!

如果报错尝试更新一下yum:

yum clean all
rpm --rebuilddb
yum update

4. 安装完之后重启一下mysql服务:

# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service

5. 输入mysql(没有设置用户和密码就可以直接连接)就可以连接到服务器

# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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>

6. 重置密码

mysql> set password for 'root'@'localhost' = password('password');
Query OK, 0 rows affected (0.00 sec)

mysql> quit

7. 用新密码重新连接MySQL

# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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>

8. 创建普通用户并授权(安装后默认一个账号是root,如果打算用root账户则可以不创建新用户)

mysql> grant all on openscannerstore.* to 'userName'@'localhost' identified by 'password';
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

9. 设置任何机器都可以访问MySQL

mysql> grant all privileges on *.* to userName@"%" identified by 'password' with grant option;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

10. 设置开机启动

vim /etc/rc.local

添加service mysqld start
或者执行下面的命令:

chkconfig --list | grep mysqld
chkconfig mysqld on

11. 设置最大连接数

编辑  /etc/my.cnf  添加如下一行,注意是在[mysqld] 区域下面添加 而不是最后,最后添加是不生效的
max_connections = 260

重启服务

# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service

连接mysql查看最大连接数

mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 260 |
+-----------------+-------+
1 row in set (0.00 sec)

12. 设置白名单

MySQL一般端口都是3306 所以可以配置防火墙的3306端口所允许的ip就可以了,如果用的是阿里云的ecs网络是专业网络,则只需要在安全组中加入3306端口并添加ip白名单即可

(完)