在CentOS下安装和使用MySQL

关键字:Linux, CentOS 6.10, MySQL 8.0.18

前言

当前我用的还是CentOS 6.10, 尽管系统自带了mysql,但是可能与我们项目需要的mysql版本不一致而导致许多问题,因此,我们可以先卸载系统中原有的mysql然后安装一个我们需要的版本。下面以安装MySQL 8.0.18版本为例进行说明。

1. 卸载已有的MySQL

在安装新的mysql之前,要先把系统中旧的版本给清理干净,否则可能会产生冲突。

[root@localhost ~]# rpm -qa | grep mysql
mysql-libs-5.1.52-1.el6_0.1.x86_64

使用下面的命令卸载mysql及其依赖

yum -y remove mysql-libs-5.1.52-1.el6_0.1.x86_64
除此之外,还要删除对应的文件,先找出需要删除的文件

[root@localhost ~]# find / -name mysql
/var/spool/mail/mysql
/var/lock/subsys/mysql
/usr/share/mysql
/usr/local/mysql-5.6.33/data/mysql
/usr/local/mysql-5.6.33/data/mysql/mysql
/usr/local/mysql-5.6.33/include/mysql
/usr/local/mysql-5.6.33/bin/mysql
/usr/lib64/mysql
/home/mysql

删除这些残余文件,如果之前的数据库还有用,请自行备份好。

rm -rf /var/lock/subsys/mysql  /usr/share/mysql    /usr/local/mysql-5.6.33  /usr/lib64/mysql   /home/mysql

2. 安装MySQL

在对性能没有极端要求下,我们一般选择yum安装或者rpm安装而不是源码安装,这两种安装方式相对来说更简单。

2.1 yum安装

要根据系统找到对应的yum文件, 可以在官网下载对应的仓库

https://dev.mysql.com/downloads/repo/yum/

MySQL官方的yum仓库

[root@localhost ~]# wget http://dev.mysql.com/get/mysql-community-release-el6-3.noarch.rpm
[root@localhost ~]# rpm -ivh mysql80-community-release-el6-3.noarch.rpm

上面的命令会自动在/etc/yum.repos.d目录下创建两个repo文件,可以看下文件的内容(节选了部分):

# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
 
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
 
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

上面的enabled=1 表示yum安装时安装该组件,否则就是不安装

实际上就是往系统中添加了两个yum源文件,因此需要更新一下yum的缓存

[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache

然后就可以直接使用命令进行安装了

[root@localhost ~]# yum install mysql-community-server

yum会根据软件的依赖从官网下载软件包,并安装所有依赖的包,直到软件完全安装完成,但是有一点儿不好的是官网下载太慢,所以这里没有使用这种方式,而是下面即将提到的rpm包手动安装。

2.2 rpm安装

首先我们在所需的目录下创建一个文件夹用来存放即将从压缩包里解压出来的文件,然后从国内的镜像源下载捆绑包。

(venv) [root@localhost ~]# mkdir MySQL8.0
(venv) [root@localhost ~]# wget http://mirrors.sohu.com/mysql/MySQL-8.0/mysql-8.0.18-1.el6.x86_64.rpm-bundle.tar
--2019-12-31 22:43:15--  http://mirrors.sohu.com/mysql/MySQL-8.0/mysql-8.0.18-1.el6.x86_64.rpm-bundle.tar
正在解析主机 mirrors.sohu.com... 123.125.123.141
正在连接 mirrors.sohu.com|123.125.123.141|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:762746880 (727M) [application/octet-stream]
正在保存至: “mysql-8.0.18-1.el6.x86_64.rpm-bundle.tar”

将捆绑包解压缩:

(venv) [root@localhost ~]# tar -xf mysql-8.0.18-1.el6.x86_64.rpm-bundle.tar -C ~/MySQL8.0
(venv) [root@localhost ~]# cd MySQL8.0/
(venv) [root@localhost MySQL8.0]# ll
总用量 744872
-rw-r--r--. 1 7155 31415  45550660 9月  23 15:19 mysql-community-client-8.0.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415    743120 9月  23 15:20 mysql-community-common-8.0.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415   7607088 9月  23 15:20 mysql-community-devel-8.0.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415   4390588 9月  23 15:20 mysql-community-libs-8.0.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415   1775476 9月  23 15:20 mysql-community-libs-compat-8.0.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415 537250496 9月  23 15:20 mysql-community-server-8.0.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415 165415576 9月  23 15:21 mysql-community-test-8.0.18-1.el6.x86_64.rpm

接下来就开始安装了,安装时注意顺序:

(venv) [root@localhost MySQL8.0]# rpm -ivh mysql-community-common-8.0.18-1.el6.x86_64.rpm 
warning: mysql-community-common-8.0.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-common ########################################### [100%]
(venv) [root@localhost MySQL8.0]# rpm -ivh mysql-community-libs-8.0.18-1.el6.x86_64.rpm 
warning: mysql-community-libs-8.0.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-libs   ########################################### [100%]
(venv) [root@localhost MySQL8.0]# rpm -ivh mysql-community-client-8.0.18-1.el6.x86_64.rpm 
warning: mysql-community-client-8.0.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-client ########################################### [100%]
(venv) [root@localhost MySQL8.0]# rpm -ivh mysql-community-server-8.0.18-1.el6.x86_64.rpm 
warning: mysql-community-server-8.0.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-server ########################################### [100%]
(venv) [root@localhost MySQL8.0]# rpm -ivh mysql-community-devel-8.0.18-1.el6.x86_64.rpm 
warning: mysql-community-devel-8.0.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-devel  ########################################### [100%]
(venv) [root@localhost MySQL8.0]#

安装完成后要对mysql进行必要的配置,这一步很重要,因为有些参数只能在数据库初始化之前进行设置,不支持初始化之后再修改,如忽略大小写就是这样的配置选项lower_case_table_names

 [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 the leading "# " to disable binary logging
 # Binary logging captures changes between backups and is enabled by
 # default. It's default setting is log_bin=binlog
 # disable_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
 #
 # Remove leading # to revert to previous value for default_authentication_plugin,
 # this will increase compatibility with older clients. For background, see:
 # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
 # default-authentication-plugin=mysql_native_password
user             = mysql
port             = 3306
datadir          = /var/lib/mysql
socket           = /var/lib/mysql/mysql.sock
log-error        = /var/log/mysqld.log
pid-file         = /var/run/mysqld/mysqld.pid

lower_case_table_names = 1

这里只做了简单的配置,没有进行优化,详细的参数设置可以参考下面的文章

https://blog.csdn.net/qq1620851849/article/details/80742780
https://blog.csdn.net/spokenInChina/article/details/45767035
https://blog.csdn.net/sinat_27933301/article/details/84113002

注意如果像我一样在配置文件了另外指定了数据库存储位置、log文件存储位置的话,需要手工创建这些目录和文件的,否则后报错。接下来就可以初始化ysql了

(venv) [root@localhost local]# mysqld --initialize

启动数据库服务:

(venv) [root@localhost log]# service mysqld start
初始化 MySQL 数据库:                                      [确定]
正在启动 mysqld:                                          [确定]

打开日志文件/var/log/mysqld.log 可以看到:

2019-12-31T18:30:16.469006Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 39451
2019-12-31T18:30:21.288663Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: F_md:xVAJ9Vx
2019-12-31T18:30:26.001672Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 39723
2019-12-31T18:30:27.712353Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-12-31T18:30:28.073479Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Communit    y Server - GPL.
2019-12-31T18:30:28.236960Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

注意第二行,有我们root账户的临时密码,这密码谁记得住,赶紧改一个

(venv) [root@localhost log]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 
 ... Failed! Error: Your password does not satisfy the current policy requirements

New password: 

Re-enter new password: 
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: 

Re-enter new password: 

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
 ... Failed! Error: Your password does not satisfy the current policy requirements

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
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? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - 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? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

由于默认的密码政策是Medium,因此要求密码中必须包含数字,大写字母,小写字母和特殊字符,密码长度不少于8位,可见默认的安全等级还是不错的,如果你只是测试的话可以将密码策略改成LOW,这样你就可以使用诸如12123344这样的简单密码了[参考文献5-6]。
至此,我们的MySQL 8.0.18已经完成了安装,接下来就简单的操作一下。

3. 进入MySQL

首先以root权限登录数据库:

(venv) [root@localhost log]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.18 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> create database kangheSPC;
Query OK, 1 row affected (0.01 sec)

mysql> create user "kangheAdmin" IDENTIFIED BY "SPC.6600242hf";
Query OK, 0 rows affected (0.02 sec)

mysql> grant all privileges on kangheSPC.* to kangheAdmin@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)

参考文献

  1. https://www.jianshu.com/p/672f316c15eb
  2. https://www.cnblogs.com/heaven-xi/p/9790494.html
  3. https://blog.csdn.net/weixin_34242819/article/details/85907120
  4. https://blog.csdn.net/engchina/article/details/91350012
  5. https://blog.csdn.net/ssiyla/article/details/82931439
  6. https://blog.csdn.net/Hello_World_QWP/article/details/79551789
  7. https://www.cnblogs.com/bethal/p/5512755.html
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342