centos7 安装mysql

安装环境或方式

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

安装mysql

顺序执行下列语句

[root@db01 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 root@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
  1. 刷新修改
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

安装过程日志

[root@db01 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]
[root@db01 mysql]# ll
total 8
-rw-r--r--. 1 root root 6140 Nov 12  2015 mysql-community-release-el7-5.noarch.rpm
[root@db01 mysql]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-release-el7-5    ################################# [100%]
[root@db01 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 <mysql-build@oss.oracle.com>"
 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!
[root@db01 mysql]# ll
total 8
-rw-r--r--. 1 root root 6140 Nov 12  2015 mysql-community-release-el7-5.noarch.rpm
[root@db01 mysql]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
s[root@db01 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,看起来像是密码没有生效

[root@db01 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的密码认证过程
[root@db01 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 ;#退出
操作如下

[root@db01 mysql]# service mysqld restart 
Redirecting to /bin/systemctl restart mysqld.service
[root@db01 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

[root@db01 mysql]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
[root@db01 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成功

远程连接mysql失败

我的工作环境是windows,通过远程工具连接mysql发现连接失败
在windows下ping远程ip192.168.45.161可以ping通,但是telnet 192.168.45.161 3306确失败了
说明远程端口3306没有开放
所以我们需要开放这个端口

  1. 查看防火墙状态
    systemctl status firewalld
[root@db01 sysconfig]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-02-12 17:54:57 CST; 1 day 21h ago
     Docs: man:firewalld(1)
 Main PID: 6201 (firewalld)
    Tasks: 2
   CGroup: /system.slice/firewalld.service
           └─6201 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
  1. 如果没有开启则开启防火墙
    systemctl start firewalld
systemctl start firewalld
  1. 开放端口
// --permanent 永久生效,没有此参数重启后失效
firewall-cmd --zone=public --add-port=3306/tcp --permanent #开放一个指定端口
firewall-cmd --zone=public --add-port=1000-2000/tcp --permanent #开放一个范围端口 

firewall-cmd –zone=public –add-port=3306/tcp –permanent

[root@db01 sysconfig]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
  1. 重启防火墙并查看端口情况
[root@db01 sysconfig]# firewall-cmd --reload
success
[root@db01 sysconfig]# firewall-cmd --zone=public --query-port=3306/tcp
yes

版权声明:本文为博主原创文章,转载请附上博文链接!

    原文作者:利伊奥克儿
    原文地址: https://www.jianshu.com/p/94a75bd3f720
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞