centos7 安装mysql

Posted lillcol

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7 安装mysql相关的知识,希望对你有一定的参考价值。

安装环境或方式

环境: centos7_64
安装方式:官网下载安装mysql-server
ps:mysql安装其实不难,但是并没有把步骤记下来,所以我把安装的步骤整理出来方便以后使用

安装mysql

顺序执行下列语句

[[email protected] mysql]# cd /usr/local/mysql
# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
# rpm -ivh mysql-community-release-el7-5.noarch.rpm
# yum install mysql-community-server

配置mysql

  1. 配置编码格式
    /etc/my.cnf 最后面添加 default-character-set =utf8
    default-character-set =utf8
  2. 设置密码
mysql> set password for ‘root‘@‘localhost‘ =password(‘123456‘);
Query OK, 0 rows affected (0.01 sec)
  1. 新建用户
mysql> create user ‘hdfs‘@‘%‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
  1. 表的所有权限赋值给位于所有IP地址的root用户
mysql> grant all privileges on *.* to [email protected]‘%‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
  1. 刷新修改
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

安装过程日志

[[email protected] mysql]# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
--2019-02-13 10:04:20--  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... dRead error (Connection reset by peer) in headers.
Retrying.

--2019-02-13 10:04:31--  (try: 2)  http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
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]
--2019-02-13 10:04:34--  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]
--2019-02-13 10:04:36--  https://repo.mysql.com//mysql-community-release-el7-5.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 173.223.96.113
Connecting to repo.mysql.com (repo.mysql.com)|173.223.96.113|: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

2019-02-13 10:04:38 (34.2 MB/s) - ‘mysql-community-release-el7-5.noarch.rpm’ saved [6140/6140]
[[email protected] mysql]# ll
total 8
-rw-r--r--. 1 root root 6140 Nov 12  2015 mysql-community-release-el7-5.noarch.rpm
[[email protected] mysql]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-release-el7-5    ################################# [100%]
[[email protected] mysql]# yum install mysql-community-server
Loaded plugins: fastestmirror, langpacks
Existing lock /var/run/yum.pid: another copy is running as pid 68490.
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: PackageKit
    Memory :  36 M RSS (860 MB VSZ)
    Started: Wed Feb 13 10:04:58 2019 - 00:07 ago
    State  : Running, pid: 68490
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: PackageKit
    Memory :  44 M RSS (868 MB VSZ)
    Started: Wed Feb 13 10:04:58 2019 - 00:09 ago
    State  : Sleeping, pid: 68490
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: PackageKit
    Memory :  47 M RSS (872 MB VSZ)
    Started: Wed Feb 13 10:04:58 2019 - 00:11 ago
    State  : Sleeping, pid: 68490
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: PackageKit
    Memory :  47 M RSS (872 MB VSZ)
    Started: Wed Feb 13 10:04:58 2019 - 00:13 ago
    State  : Sleeping, pid: 68490
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.6.43-2.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.6.43-2.el7 for package: mysql-community-server-5.6.43-2.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.6.10 for package: mysql-community-server-5.6.43-2.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.6.43-2.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.6.10 for package: mysql-community-client-5.6.43-2.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.6.43-2.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.60-1.el7_5 will be obsoleted
---> Package mysql-community-libs.x86_64 0:5.6.43-2.el7 will be obsoleting
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================
 Package                            Arch               Version                  Repository                     Size
====================================================================================================================
Installing:
 mysql-community-libs               x86_64             5.6.43-2.el7             mysql56-community             2.0 M
     replacing  mariadb-libs.x86_64 1:5.5.60-1.el7_5
 mysql-community-server             x86_64             5.6.43-2.el7             mysql56-community              59 M
Installing for dependencies:
 mysql-community-client             x86_64             5.6.43-2.el7             mysql56-community              20 M
 mysql-community-common             x86_64             5.6.43-2.el7             mysql56-community             257 k

Transaction Summary
====================================================================================================================
Install  2 Packages (+2 Dependent packages)

Total download size: 81 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql56-community/packages/mysql-community-common-5.6.43-2.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-5.6.43-2.el7.x86_64.rpm is not installed
(1/4): mysql-community-common-5.6.43-2.el7.x86_64.rpm                                        | 257 kB  00:00:03
(2/4): mysql-community-libs-5.6.43-2.el7.x86_64.rpm                                          | 2.0 MB  00:00:40
(3/4): mysql-community-client-5.6.43-2.el7.x86_64.rpm                                        |  20 MB  00:06:53
(4/4): mysql-community-server-5.6.43-2.el7.x86_64.rpm                                        |  59 MB  00:27:40
--------------------------------------------------------------------------------------------------------------------
Total                                                                                49 kB/s |  81 MB  00:28:25
Retrieving key from file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <[email protected]>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql-community-release-el7-5.noarch (installed)
 From       : file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : mysql-community-common-5.6.43-2.el7.x86_64                                                       1/5
  Installing : mysql-community-libs-5.6.43-2.el7.x86_64                                                         2/5
  Installing : mysql-community-client-5.6.43-2.el7.x86_64                                                       3/5
  Installing : mysql-community-server-5.6.43-2.el7.x86_64                                                       4/5
  Erasing    : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                             5/5
  Verifying  : mysql-community-server-5.6.43-2.el7.x86_64                                                       1/5
  Verifying  : mysql-community-common-5.6.43-2.el7.x86_64                                                       2/5
  Verifying  : mysql-community-libs-5.6.43-2.el7.x86_64                                                         3/5
  Verifying  : mysql-community-client-5.6.43-2.el7.x86_64                                                       4/5
  Verifying  : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                             5/5

Installed:
  mysql-community-libs.x86_64 0:5.6.43-2.el7              mysql-community-server.x86_64 0:5.6.43-2.el7

Dependency Installed:
  mysql-community-client.x86_64 0:5.6.43-2.el7             mysql-community-common.x86_64 0:5.6.43-2.el7

Replaced:
  mariadb-libs.x86_64 1:5.5.60-1.el7_5

Complete!
[[email protected] mysql]# ll
total 8
-rw-r--r--. 1 root root 6140 Nov 12  2015 mysql-community-release-el7-5.noarch.rpm
[[email protected] mysql]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
s[[email protected] mysql]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.43 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报错

在退出来重新连接mysql的时候报错,无论是用root还是新建的用户hdfs,看起来像是密码没有生效

[[email protected] hdfs]# mysql -uhdfs -p‘123456‘
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user ‘hdfs‘@‘localhost‘ (using password: YES)

通过查阅资料,发现这个错误一般是由密码错误引起,解决的办法自然就是重置密码。在这之前我也重置过密码,至于为什么没生效搞懂,应该是方法不对,所以在规范了重置密码后这个问题就解决了
现在连mysql都连接不上,更谈不上重置密码之类的操作了(也许有方法直接重置,但是我母鸡啊)
那么就要通过一下步骤来重置密码了

  1. MySQL的密码认证过程
[[email protected] mysql]#  vim /etc/my.cnf

在文件中 [mysqld] 下添加 skip-grant-tables #添加跳过密码验证的语句

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]
skip-grant-tables  #添加跳过密码验证的语句
#
# 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

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
default-character-set =utf8

wq 保存文档并退出
接下来
service mysqld restart #重启mysql 有些用 service mysql restart
mysql #免密码登陆
use mysql; 切换数据库
update user set password=password("123456") where user="root"; #修改密码
flush privileges; #刷新修改
quit ;#退出
操作如下

[[email protected] mysql]# service mysqld restart 
Redirecting to /bin/systemctl restart mysqld.service
[[email protected] mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.43 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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password("123456") where user="root";
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5  Changed: 5  Warnings: 0

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

mysql> quit

接下来my.cnf,去掉或者注释掉刚才添加的内容,保存退出

[mysqld]
# skip-grant-tables  #添加跳过密码验证的语句

重新启动mysql

[[email protected] mysql]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
[[email protected] mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.43 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> exit
Bye

登陆输入用户密码登陆mysql成功

本文为本人日常工作整理总结,转载请注明出处!!!















以上是关于centos7 安装mysql的主要内容,如果未能解决你的问题,请参考以下文章

Mysql系列二:Centos7.4安装mysql5.7.28源码

centos7安装mariadb(mysql的替代品)

CentOS7上安装MySQL总结

MySql之centos7.2安装mysql5.7.13

Centos7中编译安装MySQL(mysql-5.7)

基于centos7搭建MySQL数据库