一、说明
个人感觉,glibc版的,好配置多版本、多实例。同一版本也方便配置多实例。
mysql安装方式很多:
1.glibc版,tar.gz,tar.xz
2.yum安装
CentOS 8 安装 MySQL 5.7 或 MySQL 8
A Quick Guide to Using the MySQL Yum Repository
3.rpm安装
4.源码安装
5.tar包
二、下载地址
https://dev.mysql.com/downloads/mysql/
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
三、安装MySQL5.7
1.上传,解压,重命名
[root@dev1 opt]# tar -xvf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@dev1 opt]# mv mysql-5.7.30-linux-glibc2.12-x86_64 mysql-5.7.30
2.创建配置文件my.cnf
[root@dev1 opt]# cd mysql-5.7.30/
[root@dev1 mysql-5.7.30]# vi my.cnf
[client]
port=3305
[mysql]
default-character-set=utf8
[mysqld]
# MySQL服务器将监听的TCP/IP端口
port=3305
# 安装目录的路径。 通常相对于此解析所有路径。
basedir=/opt/mysql-5.7.30
# 数据库根目录的路径
datadir=/opt/mysql-5.7.30/data
# 创建新模式或表且未定义任何字符集时将使用的默认字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# MySQL服务器允许的最大并发会话数。
# 这些连接中的一个将保留给具有超级权限的用户,以允许管理员登录,即使已达到连接限制。
max_connections=151
# soket文件,本地连接时使用
socket=/opt/mysql-5.7.30/data/mysql.sock
# 建议禁用符号链接以防止各种安全风险
symbolic-links=0
# 错误日志
log-error=/opt/mysql-5.7.30/data/mysqld.log
# pid文件
pid-file=/opt/mysql-5.7.30/data/mysqld.pid
3.创建用户
-s /sbin/nologin 指定用户登入后所使用的shell。默认值为/bin/bash。/sbin/nologin,无法使用shell
-M:不要自动建立用户的登入目录。
[root@dev1 mysql-5.7.30]# useradd -s /sbin/nologin -M mysql
[root@dev1 mysql-5.7.30]# id mysql
uid=1001(mysql) gid=1001(mysql) 组=1001(mysql)
[root@dev1 mysql-5.7.30]# chown -R mysql.mysql /opt/mysql-5.7.30
4.初始化
--initialize-insecure 密码为空
[root@dev1 mysql-5.7.30]# /opt/mysql-5.7.30/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql-5.7.30 --datadir=/opt/mysql-5.7.30/data
[Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
5.创建service文件
[root@dev1 mysql-5.7.30]# vi /etc/systemd/system/mysqld57.service
[Unit]
Description=MySQL Server 5.7
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql-5.7.30/bin/mysqld --defaults-file=/opt/mysql-5.7.30/my.cnf
LimitNOFILE = max_open_files
6.启动服务
[root@dev1 mysql-5.7.30]# systemctl start mysqld57
[root@dev1 mysql-5.7.30]# netstat -lntup|grep mysqld
tcp6 0 0 :::3305 :::* LISTEN 6684/mysqld
7.客户端登录
[root@dev1 log]# /opt/mysql-5.7.30/bin/mysql -S /opt/mysql-5.7.30/data/mysql.sock
/opt/mysql-5.7.30/bin/mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
ncurses-compat-libs
[root@dev1 ~]# yum install ncurses-compat-libs
[root@dev1 log]# /opt/mysql-5.7.30/bin/mysql -S /opt/mysql-5.7.30/data/mysql.sock
mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 5.7.30 |
+-----------+
1 row in set (0.00 sec)
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3305 |
+---------------+-------+
1 row in set (0.01 sec)
8.修改密码,允许远程登录
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> select user,authentication_string,host from user;
+---------------+-------------------------------------------+-----------+
| user | authentication_string | host |
+---------------+-------------------------------------------+-----------+
| root | | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
+---------------+-------------------------------------------+-----------+
3 rows in set (0.05 sec)
mysql> ALTER user 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.00 sec)
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.06 sec)
四、安装MySQL8.0
1.上传,解压,重命名
[root@dev1 opt]# tar xvJf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
[root@dev1 opt]# mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql-8.0.20
2.创建配置文件my.cnf
[root@dev1 opt]# cd mysql-8.0.20/
[root@dev1 mysql-8.0.20]# vi my.cnf
[client]
port=3308
[mysql]
default-character-set=utf8mb4
[mysqld]
# 服务器侦听TCP/IP连接的端口号。
port=3308
# MySQL安装基础目录的路径。
basedir=/opt/mysql-8.0.20
# MySQL服务器数据目录的路径。最好将datadir值指定为绝对路径。
datadir=/opt/mysql-8.0.20/data
# 服务器默认字符集。如果设置此变量,还应设置collation_server以指定字符集的排序规则。
character-set-server=utf8mb4
# 服务器的默认排序规则。
collation_server=utf8mb4_0900_ai_ci
# 表的默认存储引擎。此变量仅为永久表设置存储引擎。
# 要为临时表设置存储引擎,请设置default_tmp_storage_engine系统变量。
default-storage-engine=InnoDB
# 允许的最大同时客户端连接数。(1-100000)
max_connections=151
# 在max_connect_errors之后,来自主机的连续连接请求在没有成功连接的情况下被中断,服务器将阻止该主机进行进一步的连接。
max_connect_errors=100
# 默认的身份验证插件。(mysql_native_password,sha256_password,caching_sha2_password)
default_authentication_plugin=caching_sha2_password
# soket文件,本地连接时使用
socket=/opt/mysql-8.0.20/data/mysql.sock
# 建议禁用符号链接以防止各种安全风险
symbolic-links=0
# 错误日志
log-error=/opt/mysql-8.0.20/data/mysqld.log
# pid文件
pid-file=/opt/mysql-8.0.20/data/mysqld.pid
3.创建用户
-s /sbin/nologin 指定用户登入后所使用的shell。默认值为/bin/bash。/sbin/nologin,无法使用shell
-M:不要自动建立用户的登入目录。
[root@dev1 mysql-8.0.20]# useradd -s /sbin/nologin -M mysql
useradd:用户“mysql”已存在
[root@dev1 mysql-8.0.20]# id mysql
uid=1001(mysql) gid=1001(mysql) 组=1001(mysql)
[root@dev1 mysql-8.0.20]# chown -R mysql.mysql /opt/mysql-8.0.20
4.初始化
--initialize-insecure 密码为空
[root@dev1 mysql-8.0.20]# /opt/mysql-8.0.20/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql-8.0.20 --datadir=/opt/mysql-8.0.20/data
[Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
5.创建service文件
[root@dev1 mysql-8.0.20]# vi /etc/systemd/system/mysqld80.service
[Unit]
Description=MySQL Server 8.0
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql-8.0.20/bin/mysqld --defaults-file=/opt/mysql-8.0.20/my.cnf
LimitNOFILE = max_open_files
6.启动服务
[root@dev1 mysql-8.0.20]# systemctl start mysqld80
[root@dev1 mysql-8.0.20]# netstat -lntup|grep mysqld
tcp6 0 0 :::33060 :::* LISTEN 9129/mysqld
tcp6 0 0 :::3305 :::* LISTEN 7479/mysqld
tcp6 0 0 :::3308 :::* LISTEN 9129/mysqld
7.客户端登录
[root@dev1 log]# /opt/mysql-8.0.20/bin/mysql -S /opt/mysql-8.0.20/data/mysql.sock
mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 8.0.20 |
+-----------+
1 row in set (0.00 sec)
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3308 |
+---------------+-------+
1 row in set (0.01 sec)
8.修改密码,允许远程登录
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> select user,authentication_string,host from user;
+------------------+------------------------------------------------------------------------+-----------+
| user | authentication_string | host |
+------------------+------------------------------------------------------------------------+-----------+
| mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost |
| mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost |
| mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost |
| root | | localhost |
+------------------+------------------------------------------------------------------------+-----------+
4 rows in set (0.00 sec)
mysql> ALTER user 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.00 sec)
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
五、文档
2.2 Installing MySQL on Unix/Linux Using Generic Binaries
2.3.4.2 Creating an Option File
5.1.2 Server Configuration Defaults
5.1.6 Server Command Options
5.1.7 Server System Variables (my.cnf中的属性的解释及默认值)
10.4 Connection Character Sets and Collations
2.2 Installing MySQL on Unix/Linux Using Generic Binaries
2.3.4.2 Creating an Option File
5.1.2 Server Configuration Defaults
5.1.7 Server Command Options
5.1.8 Server System Variables (my.cnf中的属性的解释及默认值)
10.4 Connection Character Sets and Collations
六、yum安装mysq5.7默认配置文件(配置参考)
my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[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
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# 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
七、yum安装mysq8.0默认配置文件(配置参考)
/etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
/etc/my.cnf.d/client.cnf
#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#
[client]
# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
/etc/my.cnf.d/mysql-default-authentication-plugin.cnf
#
# MySQL 8.0.4 introduced 'caching_sha2_password' as its default authentication plugin.
# It is faster and provides better security then the previous default authentication plugin.
#
# Until now (09/2018), it does not work with some other software (eg. MariaDB client, MariaDB connectors, ...)
#
# This configuration file changes MySQL default server configuration, so it behaves the same way as in MySQL 5.7.
#
# To change the behaviour back to the upstream default, comment out the following lines:
[mysqld]
default_authentication_plugin=mysql_native_password
/etc/my.cnf.d/mysql-server.cnf
#
# This group are read by MySQL server.
# Use it for options that only the server (but not clients) should see
#
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid