前言
近十年来,MySQL数据库系统变的越来越流行了,几乎成为各个互联网应用数据库系统的标配。这里先介绍一下 MySQL 数据库的安装,后续再做一些深度的主题。
准备安装文件
现在 MySQL 最新版本已经是 MySQL 8 了。所以我们这里就用 MySQL 8 来作为例子。安装 MySQL 8 的服务器是 CentOS 7 ,IP 地址为 10.110.2.72 。
首先到官网 https://dev.mysql.com/downloads/mysql/ 来下载对应版本的 RPM 安装包,选择如图所示的版本。其中 Bundle 是一个包含了各种MySQL 其他组件的包,免去了一个个单独下载的繁琐步骤。
安装
我的操作系统是 CentOS 7 最小安装的版本。按照下面的操作步骤和顺序进行,就可以将 MySQL 安装到操作系统。
yum remove mariadb-libs
yum install perl
tar -xvf mysql-8.0.14-1.el7.x86_64.rpm-bundle.tar.gz
rpm -ivh mysql-community-common-8.0.14-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.14-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-8.0.14-1.el7.x86_64.rpm
rpm -ivh mysql-community-embedded-compat-8.0.14-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.14-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.14-1.el7.x86_64.rpm
然后创建 mysql 的数据文件目录
mkdir /data/mysql
mkdir /data/mysql/logs
chown -R mysql:mysql /data/mysql
配置
修改配置文件 /etc/my.cnf 如下所示
[mysqld]
server-id=72
port=38965
character-set-server=utf8mb4
default-time_zone='+8:00'
lower_case_table_names=1
max_connections=3000
max_connect_errors=100000
default_authentication_plugin=mysql_native_password
datadir=/data/mysql/data
socket=/data/mysql/mysql.sock
log-error=/data/mysql/logs/mysqld.log
pid-file=/data/mysql/mysqld.pid
long_query_time=1
slow_query_log = 1
slow_query_log_file=/data/mysql/logs/slow.log
[client]
port=38965
default-character-set=utf8mb4
socket=/data/mysql/mysql.sock
[mysql]
no-auto-rehash
default-character-set=utf8mb4
这里将 MySQL 的服务端口设置为 38965 ,并且 server-id 设置为本机 ip 的最后一段 72 。
关闭 selinux,确保 getenforce 的值为 Permissive
setenforce 0
getenforce
Permissive
启动
通过下面的命令启动MySQL
systemctl start mysqld
如果没有问题,查看 /data/mysql/logs/mysqld.log 文件内容。
cat /data/mysql/logs/mysqld.log
2019-01-29T18:51:55.600096Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.13) initializing of server in progress as process 18964
2019-01-29T18:52:25.564666Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ;-1wtd-t6jqE
2019-01-29T18:52:37.781510Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (mysqld 8.0.13) initializing of server has completed
2019-01-29T18:52:41.606598Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 10000 (requested 15000)
2019-01-29T18:52:41.606840Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 3495 (requested 4000)
2019-01-29T18:52:41.896630Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.13) starting as process 19012
2019-01-29T18:52:44.240940Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-01-29T18:52:44.320302Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.13' socket: '/data/mysql/mysql.sock' port: 38965 MySQL Community Server - GPL.
2019-01-29T18:52:44.458457Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
其中第二行表明生成了一个临时的 root 密码 ;-1wtd-t6jqE,可以用这个密码来登录 mysql ,但是登录后首先需要修改掉这个初始的密码,否则,不运行进行其他操作
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.14 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 '新密码';
mysql>
这样,最新版本的 MySQL 8 就安装好并且能够运行了。