关键字: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/
[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)
参考文献
- https://www.jianshu.com/p/672f316c15eb
- https://www.cnblogs.com/heaven-xi/p/9790494.html
- https://blog.csdn.net/weixin_34242819/article/details/85907120
- https://blog.csdn.net/engchina/article/details/91350012
- https://blog.csdn.net/ssiyla/article/details/82931439
- https://blog.csdn.net/Hello_World_QWP/article/details/79551789
- https://www.cnblogs.com/bethal/p/5512755.html