CentOS7 MySQL安装 及遇到的常见问题

首先安装了 MySQL 5.5 后面版本不对 导致很多问题  脑子一转 又装了个当前最新 5.7 发现还是版本不对  各种so找不到或者  符号未定义  最后全卸掉装5.6,终于可以了  前后折腾两三天   (本文整理自网络,自己一路踩坑过来 行之有效)

首先 卸载老版本MySQL 

rpm -qa | grep -i mysql // 查看命令1

yum list install mysql* // 查看命令2

卸载由yum 安装的MySQL   yum list 列出来的什么  就通过 yum remove 对应的项目名称卸载掉  知道 list 不出来啥了

然后通过下面安装 MySQL 5.6

[root@iZuf67u6816pmg3fc1bx5fZ ~]# rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
Retrieving http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-release-el7-5    ################################# [100%]
[root@iZuf67u6816pmg3fc1bx5fZ ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community                  42
mysql-tools-community/x86_64      MySQL Tools Community                       51
mysql56-community/x86_64          MySQL 5.6 Community Server                 344
[root@iZuf67u6816pmg3fc1bx5fZ ~]# 
[root@iZuf67u6816pmg3fc1bx5fZ ~]# 
[root@iZuf67u6816pmg3fc1bx5fZ ~]# 
[root@iZuf67u6816pmg3fc1bx5fZ ~]# yum -y install mysql-community-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.6.37-2.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.6.37-2.el7 for package: mysql-community-server-5.6.37-2.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.6.10 for package: mysql-community-server-5.6.37-2.el7.x86_64
--> Processing Dependency: perl(Data::Dumper) for package: mysql-community-server-5.6.37-2.el7.x86_64
--> Processing Dependency: perl(DBI) for package: mysql-community-server-5.6.37-2.el7.x86_64
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.52-1.el7 will be obsoleted
---> Package mysql-community-client.x86_64 0:5.6.37-2.el7 will be obsoleting
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.6.10 for package: mysql-community-client-5.6.37-2.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.6.37-2.el7 will be installed
---> Package perl-DBI.x86_64 0:1.627-4.el7 will be installed
--> Processing Dependency: perl(RPC::PlServer) >= 0.2001 for package: perl-DBI-1.627-4.el7.x86_64
--> Processing Dependency: perl(RPC::PlClient) >= 0.2000 for package: perl-DBI-1.627-4.el7.x86_64
---> Package perl-Data-Dumper.x86_64 0:2.145-3.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.52-1.el7 will be obsoleted
---> Package mysql-community-libs.x86_64 0:5.6.37-2.el7 will be obsoleting
---> Package perl-PlRPC.noarch 0:0.2020-14.el7 will be installed
--> Processing Dependency: perl(Net::Daemon) >= 0.13 for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Test) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Log) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Compress::Zlib) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Running transaction check
---> Package perl-IO-Compress.noarch 0:2.061-2.el7 will be installed
--> Processing Dependency: perl(Compress::Raw::Zlib) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
--> Processing Dependency: perl(Compress::Raw::Bzip2) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
---> Package perl-Net-Daemon.noarch 0:0.48-5.el7 will be installed
--> Running transaction check
---> Package perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 will be installed
---> Package perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================
 Package                                   Arch                     Version                          Repository                           Size
===============================================================================================================================================
Installing:
 mysql-community-client                    x86_64                   5.6.37-2.el7                     mysql56-community                    19 M
     replacing  mariadb.x86_64 1:5.5.52-1.el7
 mysql-community-libs                      x86_64                   5.6.37-2.el7                     mysql56-community                   2.0 M
     replacing  mariadb-libs.x86_64 1:5.5.52-1.el7
 mysql-community-server                    x86_64                   5.6.37-2.el7                     mysql56-community                    59 M
Installing for dependencies:
 mysql-community-common                    x86_64                   5.6.37-2.el7                     mysql56-community                   257 k
 perl-Compress-Raw-Bzip2                   x86_64                   2.061-3.el7                      base                                 32 k
 perl-Compress-Raw-Zlib                    x86_64                   1:2.061-4.el7                    base                                 57 k
 perl-DBI                                  x86_64                   1.627-4.el7                      base                                802 k
 perl-Data-Dumper                          x86_64                   2.145-3.el7                      base                                 47 k
 perl-IO-Compress                          noarch                   2.061-2.el7                      base                                260 k
 perl-Net-Daemon                           noarch                   0.48-5.el7                       base                                 51 k
 perl-PlRPC                                noarch                   0.2020-14.el7                    base                                 36 k

Transaction Summary
===============================================================================================================================================
Install  3 Packages (+8 Dependent packages)

Total download size: 82 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql56-community/packages/mysql-community-common-5.6.37-2.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-5.6.37-2.el7.x86_64.rpm is not installed
(1/11): mysql-community-common-5.6.37-2.el7.x86_64.rpm                                                                  | 257 kB  00:00:00     
(2/11): mysql-community-libs-5.6.37-2.el7.x86_64.rpm                                                                    | 2.0 MB  00:00:01     
(3/11): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm                                                                  |  32 kB  00:00:00     
(4/11): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm                                                                   |  57 kB  00:00:00     
(5/11): perl-Data-Dumper-2.145-3.el7.x86_64.rpm                                                                         |  47 kB  00:00:00     
(6/11): perl-IO-Compress-2.061-2.el7.noarch.rpm                                                                         | 260 kB  00:00:00     
(7/11): perl-Net-Daemon-0.48-5.el7.noarch.rpm                                                                           |  51 kB  00:00:00     
(8/11): perl-PlRPC-0.2020-14.el7.noarch.rpm                                                                             |  36 kB  00:00:00     
(9/11): perl-DBI-1.627-4.el7.x86_64.rpm                                                                                 | 802 kB  00:00:00     
(10/11): mysql-community-client-5.6.37-2.el7.x86_64.rpm                                                                 |  19 MB  00:00:07     
(11/11): mysql-community-server-5.6.37-2.el7.x86_64.rpm                                                                 |  59 MB  00:00:16     
-----------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                          4.2 MB/s |  82 MB  00:00:19     
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
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : mysql-community-common-5.6.37-2.el7.x86_64                                                                                 1/13 
  Installing : perl-Data-Dumper-2.145-3.el7.x86_64                                                                                        2/13 
  Installing : mysql-community-libs-5.6.37-2.el7.x86_64                                                                                   3/13 
  Installing : mysql-community-client-5.6.37-2.el7.x86_64                                                                                 4/13 
  Installing : perl-Net-Daemon-0.48-5.el7.noarch                                                                                          5/13 
  Installing : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                                                                6/13 
  Installing : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                                                                 7/13 
  Installing : perl-IO-Compress-2.061-2.el7.noarch                                                                                        8/13 
  Installing : perl-PlRPC-0.2020-14.el7.noarch                                                                                            9/13 
  Installing : perl-DBI-1.627-4.el7.x86_64                                                                                               10/13 
  Installing : mysql-community-server-5.6.37-2.el7.x86_64                                                                                11/13 
  Erasing    : 1:mariadb-5.5.52-1.el7.x86_64                                                                                             12/13 
  Erasing    : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                        13/13 
  Verifying  : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                                                                 1/13 
  Verifying  : perl-Data-Dumper-2.145-3.el7.x86_64                                                                                        2/13 
  Verifying  : perl-IO-Compress-2.061-2.el7.noarch                                                                                        3/13 
  Verifying  : mysql-community-libs-5.6.37-2.el7.x86_64                                                                                   4/13 
  Verifying  : mysql-community-common-5.6.37-2.el7.x86_64                                                                                 5/13 
  Verifying  : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                                                                6/13 
  Verifying  : mysql-community-server-5.6.37-2.el7.x86_64                                                                                 7/13 
  Verifying  : perl-Net-Daemon-0.48-5.el7.noarch                                                                                          8/13 
  Verifying  : perl-DBI-1.627-4.el7.x86_64                                                                                                9/13 
  Verifying  : mysql-community-client-5.6.37-2.el7.x86_64                                                                                10/13 
  Verifying  : perl-PlRPC-0.2020-14.el7.noarch                                                                                           11/13 
  Verifying  : 1:mariadb-5.5.52-1.el7.x86_64                                                                                             12/13 
  Verifying  : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                        13/13 

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

Dependency Installed:
  mysql-community-common.x86_64 0:5.6.37-2.el7   perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7   perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7  
  perl-DBI.x86_64 0:1.627-4.el7                  perl-Data-Dumper.x86_64 0:2.145-3.el7          perl-IO-Compress.noarch 0:2.061-2.el7        
  perl-Net-Daemon.noarch 0:0.48-5.el7            perl-PlRPC.noarch 0:0.2020-14.el7             

Replaced:
  mariadb.x86_64 1:5.5.52-1.el7                                       mariadb-libs.x86_64 1:5.5.52-1.el7                                      

Complete!
[root@iZuf67u6816pmg3fc1bx5fZ ~]# systemctl enable mysqld
[root@iZuf67u6816pmg3fc1bx5fZ ~]# systemctl start mysqld
[root@iZuf67u6816pmg3fc1bx5fZ ~]# 
[root@iZuf67u6816pmg3fc1bx5fZ ~]# 
[root@iZuf67u6816pmg3fc1bx5fZ ~]# 
[root@iZuf67u6816pmg3fc1bx5fZ ~]# 
[root@iZuf67u6816pmg3fc1bx5fZ ~]# 
[root@iZuf67u6816pmg3fc1bx5fZ ~]# mysql_secure_installation



NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!




All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


Cleaning up...
[root@iZuf67u6816pmg3fc1bx5fZ ~]# 
[root@iZuf67u6816pmg3fc1bx5fZ ~]# 
[root@iZuf67u6816pmg3fc1bx5fZ ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 14
Server version: 5.6.37 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
rows in set (0.00 sec)

mysql>

FAQ:

1、 Invalid default value for 'CREATE_TIME'  这是因为MySQL 5.6以后timestamp设定默认值规则改变,不能为”0000 00-00 00:00:00”

首先查看sql_mode:
mysql> show session variables like '%sql_mode%';
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value                                                                                                                                     |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| sql_mode      | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
修改sql_mode,去掉NO_ZERO_IN_DATE,NO_ZERO_DATE:
mysql> set sql_mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> commit;

这种方法重启就失效了 如果需要永久有效需要在/etc/my.cnf  也就是MySQL的配置文件中指定

[mysqld]

sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

然后重启 mysql 就可以了

2、如何查看是否使用了指定目录的my.cnf

启动mysql后,我们查看mysql的进程,看看是否有设置使用指定目录的my.cnf文件,如果有则表示mysql启动时是加载了这个配置文件。

命令   ps aux|grep mysql|grep 'my.cnf'

输出

1
2
fdipzone     25174  0.0 0.0 3087244  600  ?? S   4:12下午  0:01.14 /usr/local/Cellar/mysql/5.6.24/bin/mysqld --defaults-file=/usr/local/Cellar/mysql/5.6.24/my.cnf --basedir=/usr/local/Cellar/mysql/5.6.24 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql/5.6.24/lib/plugin --bind-address=127.0.0.1 --log-error=/usr/local/var/mysql/TerrydeMacBook-Air.local.err --pid-file=/usr/local/var/mysql/TerrydeMacBook-Air.local.pid
fdipzone     25064  0.0 0.0 2452824   4  ?? S   4:12下午  0:00.03 /bin/sh /usr/local/opt/mysql/bin/mysqld_safe --defaults-file=/usr/local/Cellar/mysql/5.6.24/my.cnf --bind-address=127.0.0.1 --datadir=/usr/local/var/mysql

可以看到/usr/local/Cellar/mysql/5.6.24/my.cnf就是mysql启动加载的配置文件。如果上面的命令没有输出,表示没有设置使用指定目录的my.cnf。

查看mysql默认读取my.cnf的目录

如果没有设置使用指定目录的my.cnf,mysql启动时会读取安装目录根目录及默认目录下的my.cnf文件。查看mysql启动时读取配置文件的默认目录

命令   mysql --help|grep 'my.cnf'

输出

1
2
3
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf
/etc/my.cnf, /etc/mysql/my.cnf, /usr/local/etc/my.cnf, ~/.my.cnf

这些就是mysql默认会搜寻my.cnf的目录,顺序排前的优先。

启动时没有使用配置文件

如果没有设置使用指定目录my.cnf文件及默认读取目录没有my.cnf文件,表示mysql启动时并没有加载配置文件,而是使用默认配置。需要修改配置,可以在mysql默认读取的目录中,创建一个my.cnf文件(例如:/etc/my.cnf),把需要修改的配置内容写入,重启mysql后即可生效。

3、mysql远程连接 Host * is not allowed to connect to this MySQL server
mysql -u root -p
mysql>use mysql;
mysql>update user set host =%'where user =’root’;
mysql>flush privileges;

具体分析
1、在本机登入mysql后,更改“mysql”数据库里的“user”表里的“host”项,从”localhost”改为'%'。
mysql>
mysql>use mysql;
mysql>select 'host' from user where user='root';     
#查看mysql库中的user表的host值(即可进行连接访问的主机/IP名称)

mysql>update user set host = '%' where user ='root';
#修改host值(以通配符%的内容增加主机/IP地址,当然也可以直接增加某个特定IP地址,如果执行update语句时出现ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY' 错误,需要select host from user where user = 'root';
查看一下host是否已经有了%这个值,如果有了直接执行下面的flush privileges;即可)

mysql>flush privileges;
mysql>select host,user from user where user='root';
mysql>quit
4、ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)

启动mysql =》service mysqld start 出现这个错误    首先停止mysql服务 service mysqld stop

再以不检查权限的方式启动 mysqld --skip-grant-tables &
又出现以下错误:
[ERROR] Fatal error: Please read “Security” section of the manual to find out how to run mysqld as root!

执行命令以root权限启动
mysqld --user=root --skip-grant-tables &

登录mysql      mysql –uroot

设置密码
UPDATE mysql.user SET authentication_string=PASSWORD('密码') where USER='root';
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
SET PASSWORD FOR root=PASSWORD('密码');
flush privileges; // 刷新设置立即生效
exit // 退出,或者使用 quit 命令

再次进入
mysql -uroot –p // 会提示输入密码  成功则密码设置完成了

设置root权限的远程访问
grant all privileges on *.* to root@"%" identified by "密码";
flush privileges; // 刷新设置立即生效

额外学习点   systemctl  好像蛮好用的样子

systemctl is-enabled iptables.service
systemctl is-enabled servicename.service #查询服务是否开机启动
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
systemctl --failed #显示启动失败的服务

 
原文地址:https://www.cnblogs.com/lesten/p/9627577.html