第二十二章 Centos7下 Mysql 8.0.24 二进制安装

一、需求

公司由于安装jira服务,mariadb数据库已不能满足jira的使用,因此需安装新的mysql8.0,迁移mariadb的数据来保证jira的正常安装。

二、准备工作

#关闭防火墙
[root@dxg-ml ~]# systemctl stop firewalld.service

三、安装依赖

#MySQL依赖于libaio库
[root@dxg-ml ~]# yum search libaio
[root@dxg-ml ~]# yum install libaio -y

四、下载Mysql新版本

[root@dxg-ml ~]# cd /data/software
[root@dxg-ml software]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.24-linux-glibc2.12-x86_64.tar.xz

五、新建用户和用户组

不指定用户组时,默认生成一个同名用户组并加入 -r,指定为系统用户 -s,指定登录shell
[root@dxg-ml software]# groupadd mysql
[root@dxg-ml software]# useradd -r -g mysql -s /bin/false mysql

六、解压Mysql安装包

#解压安装包到/usr/local目录下
[root@dxg-ml software]# tar -xvf mysql-8.0.24-linux-glibc2.12-x86_64.tar.xz  -C /usr/local

#进入/usr/local目录
[root@dxg-ml software]# cd /usr/local/

#查看安装包
[root@dxg-ml local]# ll
总用量 8
drwxr-xr-x  9 root root  129 6月   9 17:33 mysql-8.0.24-linux-glibc2.12-x86_64

#重命名
[root@dxg-ml local]# mv mysql-8.0.24-linux-glibc2.12-x86_64  mysql

七、新建Mysql存放目录

#Mysql配置文件目录
[root@dxg-ml local]# mkdir /usr/local/mysql/etc -p

#Mysql数据目录
[root@dxg-ml software]# mkdir /usr/local/mysql/data -p
ps:数据目录不需要建立,配置文件指定时会自动创建数据目录。

#Mysql日志目录
[root@dxg-ml local]# mkdir /usr/local/mysql/log -p

#授权目录用户
[root@dxg-ml data]# chown -R mysql:mysql /usr/local/mysql

八、配置mysql配置文件

[root@dxg-ml bin]# cd /usr/local/mysql/etc/

[root@dxg-ml etc]# cat my.cnf
[client]
port    = 13306
socket = /usr/local/mysql/data/mysql.sock

[mysql]
prompt="u@h R:m:s [d]> "
no-auto-rehash

[mysqld]
user    = mysql
port    = 13306
admin_address = 127.0.0.1
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket    = /usr/local/mysql/data/mysql.sock
pid-file = /usr/local/mysql/data/mysqld.pid
character-set-server = utf8mb4
skip_name_resolve = 1
#这个就是用之前的身份认证插件
default-authentication-plugin = mysql_native_password
#保证日志的时间正确
log_timestamps = SYSTEM

#replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%

# Two-Master configure
#server-1
#auto-increment-offset = 1
#auto-increment-increment = 2

#server-2
#auto-increment-offset = 2
#auto-increment-increment = 2


# semi sync replication settings #
#plugin_dir = /usr/local/mysql/lib/mysql/plugin
#plugin_load = "validate_password.so;rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
plugin_dir =/usr/local/mysql/lib/plugin #官方版本的路径
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" #官方版本的路径

slave_parallel_workers = 4
slave_parallel_type = LOGICAL_CLOCK

open_files_limit    = 65535
back_log = 1024
max_connections = 1024
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 1536
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
slow_query_log = 1
log_timestamps = SYSTEM
slow_query_log_file = /usr/local/mysql/log/slow.log
log-error = /usr/local/mysql/log/error.log
long_query_time = 0.1
log_queries_not_using_indexes =1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1
server-id = 3306
log-bin = mysql-bin
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
binlog_expire_logs_seconds=2592000
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'
binlog_format = row
binlog_row_image=FULL
binlog_checksum = 1
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30

#transaction_isolation = REPEATABLE-READ
transaction_isolation = READ-COMMITTED
#innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 2867M
innodb_buffer_pool_instances = 4
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1G:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 3
innodb_max_undo_log_size = 4G
# innodb_undo_directory = /usr/local/mysql/log/undolog
innodb_undo_tablespaces = 95

# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_sync = 0
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
innodb_stats_on_metadata = 0

# some var for MySQL 8
log_error_verbosity = 3
innodb_print_ddl_logs = 1
binlog_expire_logs_seconds = 2592000
#innodb_dedicated_server = 0

innodb_status_file = 1
# 注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log-error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 0

#performance_schema
performance_schema = 1
performance_schema_instrument = '%memory%=on'
performance_schema_instrument = '%lock%=on'

#innodb monitor
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"
#validate_password_policy=LOW

[mysqldump]
quick
max_allowed_packet = 32M

[mysqld_safe]
#malloc-lib=/usr/local/mysql/lib/jmalloc.so
nice=-19
open-files-limit=65535

九、初始化数据库

[root@dxg-ml local]# cd /usr/local/mysql/bin/

[root@dxg-ml bin]# ./mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2021-06-09T18:04:26.063193+08:00 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-06-09T18:04:36.855062+08:00 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-06-09T18:04:55.736783+08:00 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: gop;#_4qnSgq

十、启动数据库

1.命令行界面启动

#1.进入mysql配置目录
[root@dxg-ml local]# cd /usr/local/mysql/bin/

#2.命令行启动mysql服务
[root@dxg-ml bin]# ./mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf &

#3.验证服务
[root@dxg-ml bin]# ps -ef |grep 13306
root       694 25542  0 18:54 pts/0    00:00:00 grep --color=auto 13306
mysql    29542 27905  0 18:31 pts/0    00:00:05 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/log/error.log --open-files-limit=65535 --pid-file=/usr/local/mysql/data/mysqld.pid --socket=/usr/local/mysql/data/mysql.sock --port=13306

[root@dxg-ml bin]# netstat  -lntp |grep 13306
tcp6       0      0 :::13306                :::*                    LISTEN      29542/mysqld

2.配置init启动

#1.拷贝mysql配置文件到init目录下
[root@dxg-ml bin]# cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld

#2.启动mysql
[root@dxg-ml bin]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!

#3.验证服务状态
[root@dxg-ml bin]# ps -ef |grep 13306
root       694 25542  0 18:54 pts/0    00:00:00 grep --color=auto 13306
mysql    29542 27905  0 18:31 pts/0    00:00:05 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/log/error.log --open-files-limit=65535 --pid-file=/usr/local/mysql/data/mysqld.pid --socket=/usr/local/mysql/data/mysql.sock --port=13306

[root@dxg-ml bin]# netstat  -lntp |grep 13306
tcp6       0      0 :::13306                :::*                    LISTEN      29542/mysqld

3.配置system启动

#1.配置system启动
[root@dxg-ml bin]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
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=/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf
LimitNOFILE = 5000
#Restart=on-failure
#RestartPreventExitStatus=1
#PrivateTmp=false

#2.重新加载启动文件列表
[root@dxg-ml bin]# systemctl daemon-reload

ps:服务用哪种方式启动就需要用哪种方式去停止,错的启动或者停止服务方式会造成不可解决的错误。
#3.停止init进程
[root@dxg-ml bin]# /etc/init.d/mysqld stop

#4.启动mysql服务
[root@dxg-ml bin]# systemctl start mysqld.service 

#5.查看服务状态
[root@dxg-ml bin]# systemctl status mysqld.service

[root@dxg-ml bin]# ps -ef |grep 13306
root       694 25542  0 18:54 pts/0    00:00:00 grep --color=auto 13306
mysql    29542 27905  0 18:31 pts/0    00:00:05 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/log/error.log --open-files-limit=65535 --pid-file=/usr/local/mysql/data/mysqld.pid --socket=/usr/local/mysql/data/mysql.sock --port=13306

[root@dxg-ml bin]# netstat  -lntp |grep 13306
tcp6       0      0 :::13306                :::*                    LISTEN      29542/mysqld

十一、配置环境变量

#配置环境变量
[root@dxg-ml bin]# vim /etc/profile.d/mysqld.sh
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

#重载环境变量
[root@dxg-ml bin]#source /etc/profile.d/mysqld.sh

十二、登录数据库

1.首次登录数据库

#登录数据库
[root@dxg-ml bin]# /usr/local/mysql/bin/mysql --socket=/usr/local/mysql/data/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 7
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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>

2.常用命令登录数据库

#登录并配置远程登录
[root@dxg-ml bin]# mysql -P 13306 -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8
Server version: 8.0.24

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.

root@localhost 18:35:  [(none)]>

#查看数据库提示你必须重置你的密码
root@localhost 18:36:  [(none)]> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

#修改数据库密码
root@localhost 18:36:  [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.10 sec)

#再次查看数据库
root@localhost 18:38:  [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.09 sec)

#使用数据库
root@localhost 18:38:  [(none)]> use mysql;
Database changed

#这里就可以看到root@localhost这里的密码已经是mysql_native_password方式了
root@localhost 18:39:  [mysql]> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host      | user             | authentication_string                                                  | plugin                |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | root             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B                              | mysql_native_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
4 rows in set (0.00 sec)

#创建一个远程用户登录
root@localhost 18:42:  [mysql]> create user 'root'@'%' identified by 'root';
Query OK, 0 rows affected (0.24 sec)

root@localhost 18:43:  [mysql]> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.06 sec)

root@localhost 18:44:  [mysql]> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host      | user             | authentication_string                                                  | plugin                |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| %         | root             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B                              | mysql_native_password |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | root             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B                              | mysql_native_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
5 rows in set (0.00 sec)

#退出数据库
root@localhost 18:44:  [mysql]> quit
Bye

十三、迁移数据库目录

1.背景

因mysql 8.0 安装在系统盘目录,长久会导致磁盘空间不足,因此需要迁移数据库相关目录

2.停止数据库

#1.查看数据库进程
[root@dxg-ml mysql]# ps -ef |grep 13306
root     22254 17463  0 10:01 pts/1    00:00:00 grep --color=auto 13306
mysql    29542     1  0 6月09 ?       00:02:22 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/log/error.log --open-files-limit=65535 --pid-file=/usr/local/mysql/data/mysqld.pid --socket=/usr/local/mysql/data/mysql.sock --port=13306
您在 /var/spool/mail/root 中有邮件

#杀死数据库进程
[root@dxg-ml mysql]# kill -9 29542

#3.查看数据库进程
[root@dxg-ml mysql]# ps -ef |grep 13306
root     22281 17463  0 10:01 pts/1    00:00:00 grep --color=auto 13306

3.新建数据库存放目录

[root@dxg-ml ~]# mkdir /data/mysql-8.0.24
[root@dxg-ml ~]# cd /data/mysql-8.0.24/

4.移动相关数据目录

[root@dxg-ml mysql-8.0.24]# mv /usr/local/mysql/data ./
[root@dxg-ml mysql-8.0.24]# mv /usr/local/mysql/etc ./
[root@dxg-ml mysql-8.0.24]# mv /usr/local/mysql/log ./

5.配置数据库软连接

[root@dxg-ml mysql-8.0.24]# ln -s /data/mysql-8.0.24/data /usr/local/mysql/
[root@dxg-ml mysql-8.0.24]# ln -s /data/mysql-8.0.24/log /usr/local/mysql/
[root@dxg-ml mysql-8.0.24]# ln -s /data/mysql-8.0.24/etc /usr/local/mysql/

#查看相关软连接
[root@dxg-ml mysql-8.0.24]# cd /usr/local/mysql/
[root@dxg-ml mysql]# ll
总用量 284
drwxr-xr-x  2 mysql mysql   4096 6月   9 19:06 bin
lrwxrwxrwx  1 root  root      23 6月  10 09:54 data -> /data/mysql-8.0.24/data
drwxr-xr-x  2 mysql mysql     55 3月  26 03:42 docs
lrwxrwxrwx  1 root  root      23 6月  10 09:54 etc -> /data/mysql-8.0.24/etc
drwxr-xr-x  3 mysql mysql    282 3月  26 03:42 include
drwxr-xr-x  6 mysql mysql    201 3月  26 03:42 lib
-rw-r--r--  1 mysql mysql 274942 3月  22 16:44 LICENSE
lrwxrwxrwx  1 root  root      22 6月  10 09:54 log -> /data/mysql-8.0.24/log
drwxr-xr-x  4 mysql mysql     30 3月  26 03:42 man
-rw-r--r--  1 mysql mysql    666 3月  22 16:44 README
drwxr-xr-x 28 mysql mysql   4096 3月  26 03:42 share
drwxr-xr-x  2 mysql mysql     77 3月  26 03:42 support-files

6.重启服务

#1.重启服务
[root@dxg-ml mysql]# systemctl  restart  mysqld.service

#2.查看服务状态
[root@dxg-ml mysql]# systemctl  status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since 四 2021-06-10 10:01:40 CST; 5s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 22307 (mysqld_safe)
   CGroup: /system.slice/mysqld.service
           ├─22307 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf
           └─23949 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/loca...

6月 10 10:01:40 dxg-ml systemd[1]: Started MySQL Server.
6月 10 10:01:40 dxg-ml systemd[1]: Starting MySQL Server...
6月 10 10:01:41 dxg-ml mysqld_safe[22307]: 2021-06-10T02:01:41.972907Z mysqld_safe Logging to '/usr/local/mysql/log/error.log'.
6月 10 10:01:41 dxg-ml mysqld_safe[22307]: 2021-06-10T02:01:41.991896Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

7.登录数据库

[root@dxg-ml mysql]# mysql -P 13306 -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.

root@localhost 10:02:  [(none)]>
root@localhost 10:02:  [(none)]>
root@localhost 10:02:  [(none)]>
root@localhost 10:02:  [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.18 sec)

root@localhost 10:02:  [(none)]>
root@localhost 10:02:  [(none)]>
root@localhost 10:02:  [(none)]>
root@localhost 10:02:  [(none)]> quit
Bye
原文地址:https://www.cnblogs.com/jhno1/p/15324336.html