CentOS下MySQL安装

目录

1、查看是否有安装包

[root@oracle ~]# rpm -qa | grep -i mysql

2、卸载软件安装包

[root@oracle ~]# rpm -ev mysql-libs-* --nodeps

3、清理MySQL相关目录

[root@oracle ~]# find / -name mysql
/usr/lib64/mysql
/usr/share/mysql
/etc/selinux/targeted/active/modules/100/mysql
[root@oracle ~]# rm -rf /usr/lib64/mysql
[root@oracle ~]# rm -rf /usr/share/mysql

4、查看当前Linux操作系统版本

[root@oracle ~]# lsb_release -a
LSB Version:  :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:  CentOS Linux release 7.2.1511 (Core)
Release:  7.2.1511
Codename: Core

4.1、如果lsb_release命令用不了,安装一下

[root@oracle ~]# yum install redhat-lsb -y

4.2、如果不想安装lsb_release命令,也可直接使用如下命令

cat /etc/redhat-release

5、依据操作系统版本,下载合适的安装包

[root@oracle ~]# wget https://downloads.mysql.com/archives/get/file/mysql-community-common-5.7.24-1.el7.x86_64.rpm
[root@oracle ~]# wget https://downloads.mysql.com/archives/get/file/mysql-community-libs-5.7.24-1.el7.x86_64.rpm
[root@oracle ~]# wget https://downloads.mysql.com/archives/get/file/mysql-community-client-5.7.24-1.el7.x86_64.rpm
[root@oracle ~]# wget https://downloads.mysql.com/archives/get/file/mysql-community-server-5.7.24-1.el7.x86_64.rpm

6、安装(注意安装先后顺序)

6.1、顺序安装

[root@oracle ~]# rpm -ivh mysql-community-common-5.7.24-1.el7.x86_64.rpm
[root@oracle ~]# rpm -ivh mysql-community-libs-5.7.24-1.el7.x86_64.rpm
[root@oracle ~]# rpm -ivh mysql-community-client-5.7.24-1.el7.x86_64.rpm
[root@oracle ~]# rpm -ivh mysql-community-server-5.7.24-1.el7.x86_64.rpm

6.2、错误一

[root@oracle ~]# rpm -ivh mysql-community-libs-5.7.24-1.el7.x86_64.rpm
warning: mysql-community-libs-5.7.24-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
        mariadb-libs is obsoleted by mysql-community-libs-5.7.21-1.el7.x86_64
        mariadb-libs is obsoleted by mysql-community-libs-compat-5.7.21-1.el7.x86_64
        perl(Data::Dumper) is needed by mysql-community-test-5.7.21-1.el7.x86_64
        perl(JSON) is needed by mysql-community-test-5.7.21-1.el7.x86_64

如上的报错,由于centos 7默认是mariadb数据库,卸载mariadb

[root@seiang software]# rpm -qa | grep mariadb
mariadb-libs-5.5.41-2.el7_0.x86_64
[root@oracle ~]# rpm -ev mariadb-libs-5.5.41-2.el7_0.x86_64 --nodeps

6.3、错误二,安装mysql-community-server-5.7.24-1.el7.x86_64.rpm失败,出现依赖libaio错误

[root@oracle ~]# rpm -ivh mysql-community-server-5.7.24-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.24-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
  libaio.so.1()(64bit) is needed by mysql-community-server-5.7.24-1.el7.x86_64
  libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-5.7.24-1.el7.x86_64
  libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-5.7.24-1.el7.x86_64
[root@oracle ~]# yum install libaio
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================
 Package                        Arch                           Version                                   Repository                    Size
============================================================================================================================================
Installing:
 libaio                         x86_64                         0.3.109-13.el7                            base                          24 k

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

Total download size: 24 k
Installed size: 38 k
Is this ok [y/d/N]: yes
Downloading packages:
libaio-0.3.109-13.el7.x86_64.rpm                                                                                     |  24 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libaio-0.3.109-13.el7.x86_64                                                                                             1/1 
  Verifying  : libaio-0.3.109-13.el7.x86_64                                                                                             1/1 

Installed:
  libaio.x86_64 0:0.3.109-13.el7                                                                                                            

Complete!
[root@oracle ~]# rpm -ivh mysql-community-server-5.7.24-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.24-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-server-5.7.24-1.e################################# [100%]

7、启动、停止、查看状态

[root@oracle ~]# service mysqld start
[root@oracle ~]# service mysqld stop
[root@oracle ~]# service mysqld status

8、密码设置

8.1、修改配置文件

修改/etc/my.cnf文件,如下

[mysqld]
skip-grant-tables

8.2、重启MySQL服务

[root@oracle ~]# service mysqld stop
[root@oracle ~]# service mysqld start

8.3、设置初始化密码

[root@HXY soft]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '1A2b3!xx';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

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

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '1A2b3!xx';
Query OK, 0 rows affected (0.00 sec)

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

8.4、密码登录

[root@oracle ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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.5、设置编码

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

[mysql]
default-character-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
# skip-grant-tables
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8

# 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

9、创建用户

-- 查看当前所有用户
mysql> SELECT `Host`, `User` FROM `user`;

-- 修改指定密码的验证强度等级为低
mysql> set global validate_password.policy=LOW;

-- 创建用户并指定初始化密码
mysql> CREATE USER 'tom'@'localhost' IDENTIFIED BY '12345678';

-- 授权
mysql> GRANT ALL ON `db_tom`.* TO 'tom'@'localhost' WITH GRANT OPTION;

-- 刷新
mysql> flush privileges;

10、修改root账号密码

mysql> UPDATE `mysql`.`User` SET `authentication_string` = PASSWORD('root123') WHERE `User` = 'root';

mysql> flush privileges;
posted @ 2019-04-09 23:26  Bruce.Chang.Lee  阅读(352)  评论(0)    收藏  举报