一、准备安装包和卸载旧版
上传mysql-8.0.21-1.el7.x86_64.rpm-bundle.tar文件到服务器
卸载旧版mysql和mariadb,避免冲突
rpm -qa|grep mariadb
rpm -qa|grep mysql
通过rpm -e --nodeps命令卸载
[root@testmachine install]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@testmachine install]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
二、解压tar包并通过rpm命令进行安装
解压 tar -xvf mysql-8.0.21-1.el7.x86_64.rpm-bundle.tar
[root@testmachine install]# tar -xvf mysql-8.0.21-1.el7.x86_64.rpm-bundle.tar
mysql-community-common-8.0.21-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.21-1.el7.x86_64.rpm
mysql-community-libs-8.0.21-1.el7.x86_64.rpm
mysql-community-devel-8.0.21-1.el7.x86_64.rpm
mysql-community-server-8.0.21-1.el7.x86_64.rpm
mysql-community-client-8.0.21-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm
mysql-community-test-8.0.21-1.el7.x86_64.rpm
使用命令rpm -ivh进行安装操作? 按照common–>lib–>client–>server这个顺序安装
rpm -ivh mysql-community-common-8.0.21-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.21-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.21-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.21-1.el7.x86_64.rpm
过程参考下图
[root@testmachine install]# rpm -ivh mysql-community-common-8.0.21-1.el7.x86_64.rpm
warning: mysql-community-common-8.0.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-common-8.0.21-1.e################################# [100%]
[root@testmachine install]# rpm -ivh mysql-community-libs-8.0.21-1.el7.x86_64.rpm
warning: mysql-community-libs-8.0.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-libs-8.0.21-1.el7################################# [100%]
[root@testmachine install]# rpm -ivh mysql-community-client-8.0.21-1.el7.x86_64.rpm
warning: mysql-community-client-8.0.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-client-8.0.21-1.e################################# [100%]
[root@testmachine install]# rpm -ivh mysql-community-server-8.0.21-1.el7.x86_64.rpm
warning: mysql-community-server-8.0.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-server-8.0.21-1.e################################# [100%]
[root@testmachine install]#
三、配置数据库
执行命令初始化
mysqld --initialize --console --user=mysql
启动数据库?
systemctl start mysqld.service
查看数据库状态?systemctl status mysqld.service
[root@testmachine bin]# systemctl status mysqld
?mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-09-23 17:31:19 CST; 54s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3528 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3602 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
忖3602 /usr/sbin/mysqld
Sep 23 17:31:12 testmachine systemd[1]: Starting MySQL Server...
Sep 23 17:31:19 testmachine systemd[1]: Started MySQL Server.
在日志中获取数据库临时密码 (不知道路径的可以查看配置文件/etc/my.cnf)
[root@testmachine bin]# cat /var/log/mysqld.log
2021-09-23T09:29:26.350384Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.21) initializing of server in progress as process 3468
2021-09-23T09:29:26.361836Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-09-23T09:29:27.166842Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-09-23T09:29:28.709599Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: DyeBakQ9<GdE
2021-09-23T09:31:12.547649Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.21) initializing of server in progress as process 3555
2021-09-23T09:31:12.559747Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-09-23T09:31:13.150743Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-09-23T09:31:15.170523Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: qYyXQ&fCX9vk
2021-09-23T09:31:19.029693Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.21) starting as process 3602
2021-09-23T09:31:19.055801Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-09-23T09:31:19.469806Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-09-23T09:31:19.675098Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2021-09-23T09:31:19.950673Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2021-09-23T09:31:19.950985Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2021-09-23T09:31:19.989893Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server - GPL.
登录进数据库
mysql -u root -p
输入临时密码 成功进入
修改密码
ALTER USER root@localhost identified by '密码';
授权远程登录
USE MYSQL;
update user set host = '%' where user = 'root';
刷新权限
FLUSH PRIVILEGES;
CTRL+D退出
[root@testmachine bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.21
Copyright (c) 2000, 2020, 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;
Database changed
mysql> ALTER USER root@localhost identified by 'GBs01Y4$P';
Query OK, 0 rows affected (0.01 sec)
mysql> update user set host = '%' where user = 'root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
至此数据库安装完成
四、设置开机启动和关闭防火墙
chkconfig mysqld on
添加软连接
ln -s /usr/local/mysql/bin/mysql /usr/bin
永久关闭SELINUX
vi /etc/selinux/config
将SELINUX=enforcing改为SELINUX=disabled 设置后需要重启才能生效?
[root@testmachine ~]# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
查看selinux状态 sestatus
[root@testmachine ~]# sestatus
SELinux status: disabled
五、卸载数据库
1.查看系统中是否以rpm包安装的mysql
输入命令?rpm -qa|grep mysql
[root@testmachine ~]# rpm -qa | grep mysql
mysql-community-libs-8.0.21-1.el7.x86_64
mysql-community-client-8.0.21-1.el7.x86_64
mysql-community-server-8.0.21-1.el7.x86_64
mysql-community-common-8.0.21-1.el7.x86_64
[root@testmachine ~]#
2.通过rpm -e 卸载软件包
rpm -e --nodeps mysql-community-common-8.0.21-1.el7.x86_64.rpm
rpm -e --nodeps mysql-community-libs-8.0.21-1.el7.x86_64.rpm
rpm -e --nodeps mysql-community-client-8.0.21-1.el7.x86_64.rpm
rpm -e --nodeps mysql-community-server-8.0.21-1.el7.x86_64.rpm
[root@testmachine ~]# rpm -e --nodeps mysql-community-common-8.0.21-1.el7.x86_64
[root@testmachine ~]# rpm -e --nodeps mysql-community-libs-8.0.21-1.el7.x86_64
[root@testmachine ~]# rpm -e --nodeps mysql-community-client-8.0.21-1.el7.x86_64
[root@testmachine ~]# rpm -e --nodeps mysql-community-server-8.0.21-1.el7.x86_64
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave
卸载完了可以再次运行rpm -qa|grep mysql检查
3.mysql程序命令进行删除相关目录
[root@testmachine ~]# whereis mysql
mysql: /usr/lib64/mysql
[root@testmachine ~]# rm -rf /usr/lib64/mysql
[root@testmachine ~]#
卸载完成
六、修改数据库的数据目录datadir
停止mysql,并确保它成功停止了
[root@testmachine ~]# systemctl stop mysqld.service
[root@CNGSCAPL-VPD403 ~]# systemctl status mysqld.service
?mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Thu 2021-09-23 23:17:28 CST; 12s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 5100 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 5076 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 5100 (code=exited, status=0/SUCCESS)
Status: "Server shutdown complete"
Sep 23 22:46:14 testmachine systemd[1]: Starting MySQL Server...
Sep 23 22:46:17 testmachine systemd[1]: Started MySQL Server.
Sep 23 23:17:25 testmachine systemd[1]: Stopping MySQL Server...
Sep 23 23:17:28 testmachine systemd[1]: Stopped MySQL Server.
在新位置创建目录并将所有权更改到mysql下
[root@testmachine ~]# mkdir -p /data/mysql
[root@testmachine ~]# chown -R mysql:mysql /data/mysql
[root@testmachine ~]# rsync -av /var/lib/mysql /data/mysql/
将文件移动到新的数据目录中: rsync -av /var/lib/mysql /data/mysql/
[root@testmachine ~]# rsync -av /var/lib/mysql /data/mysql/
sending incremental file list
mysql/
mysql/#ib_16384_0.dblwr
mysql/#ib_16384_1.dblwr
mysql/auto.cnf
mysql/binlog.000001
mysql/binlog.000002
mysql/binlog.000003
mysql/binlog.000004
mysql/binlog.index
mysql/ca-key.pem
mysql/ca.pem
mysql/client-cert.pem
mysql/client-key.pem
mysql/ib_buffer_pool
mysql/ib_logfile0
mysql/ib_logfile1
mysql/ibdata1
mysql/mysql.ibd
mysql/private_key.pem
mysql/public_key.pem
mysql/server-cert.pem
mysql/server-key.pem
mysql/undo_001
mysql/undo_002
mysql/#innodb_temp/
mysql/mysql/
mysql/mysql/general_log.CSM
mysql/mysql/general_log.CSV
mysql/mysql/general_log_207.sdi
mysql/mysql/slow_log.CSM
mysql/mysql/slow_log.CSV
mysql/mysql/slow_log_208.sdi
mysql/performance_schema/
mysql/performance_schema/accounts_142.sdi
mysql/performance_schema/binary_log_trans_184.sdi
mysql/performance_schema/cond_instances_81.sdi
mysql/performance_schema/data_lock_waits_158.sdi
mysql/performance_schema/data_locks_157.sdi
mysql/performance_schema/events_errors_su_136.sdi
mysql/performance_schema/events_errors_su_137.sdi
mysql/performance_schema/events_errors_su_138.sdi
mysql/performance_schema/events_errors_su_139.sdi
mysql/performance_schema/events_errors_su_140.sdi
mysql/performance_schema/events_stages_cu_108.sdi
mysql/performance_schema/events_stages_hi_109.sdi
mysql/performance_schema/events_stages_hi_110.sdi
mysql/performance_schema/events_stages_su_111.sdi
mysql/performance_schema/events_stages_su_112.sdi
mysql/performance_schema/events_stages_su_113.sdi
mysql/performance_schema/events_stages_su_114.sdi
mysql/performance_schema/events_stages_su_115.sdi
mysql/performance_schema/events_statement_116.sdi
mysql/performance_schema/events_statement_117.sdi
mysql/performance_schema/events_statement_118.sdi
mysql/performance_schema/events_statement_119.sdi
mysql/performance_schema/events_statement_120.sdi
mysql/performance_schema/events_statement_121.sdi
mysql/performance_schema/events_statement_122.sdi
mysql/performance_schema/events_statement_123.sdi
mysql/performance_schema/events_statement_124.sdi
mysql/performance_schema/events_statement_125.sdi
mysql/performance_schema/events_statement_126.sdi
mysql/performance_schema/events_statement_127.sdi
mysql/performance_schema/events_transacti_128.sdi
mysql/performance_schema/events_transacti_129.sdi
mysql/performance_schema/events_transacti_130.sdi
mysql/performance_schema/events_transacti_131.sdi
mysql/performance_schema/events_transacti_132.sdi
mysql/performance_schema/events_transacti_133.sdi
mysql/performance_schema/events_transacti_134.sdi
mysql/performance_schema/events_transacti_135.sdi
mysql/performance_schema/events_waits_cur_82.sdi
mysql/performance_schema/events_waits_his_83.sdi
mysql/performance_schema/events_waits_his_84.sdi
mysql/performance_schema/events_waits_sum_85.sdi
mysql/performance_schema/events_waits_sum_86.sdi
mysql/performance_schema/events_waits_sum_87.sdi
mysql/performance_schema/events_waits_sum_88.sdi
mysql/performance_schema/events_waits_sum_89.sdi
mysql/performance_schema/events_waits_sum_90.sdi
mysql/performance_schema/file_instances_91.sdi
mysql/performance_schema/file_summary_by__92.sdi
mysql/performance_schema/file_summary_by__93.sdi
mysql/performance_schema/global_status_176.sdi
mysql/performance_schema/global_variables_179.sdi
mysql/performance_schema/host_cache_94.sdi
mysql/performance_schema/hosts_143.sdi
mysql/performance_schema/keyring_keys_149.sdi
mysql/performance_schema/log_status_169.sdi
mysql/performance_schema/memory_summary_b_151.sdi
mysql/performance_schema/memory_summary_b_152.sdi
mysql/performance_schema/memory_summary_b_153.sdi
mysql/performance_schema/memory_summary_b_154.sdi
mysql/performance_schema/memory_summary_g_150.sdi
mysql/performance_schema/metadata_locks_156.sdi
mysql/performance_schema/mutex_instances_95.sdi
mysql/performance_schema/objects_summary__96.sdi
mysql/performance_schema/performance_time_97.sdi
mysql/performance_schema/persisted_variab_182.sdi
mysql/performance_schema/prepared_stateme_170.sdi
mysql/performance_schema/replication_appl_162.sdi
mysql/performance_schema/replication_appl_163.sdi
mysql/performance_schema/replication_appl_164.sdi
mysql/performance_schema/replication_appl_165.sdi
mysql/performance_schema/replication_appl_167.sdi
mysql/performance_schema/replication_appl_168.sdi
mysql/performance_schema/replication_conn_159.sdi
mysql/performance_schema/replication_conn_161.sdi
mysql/performance_schema/replication_grou_160.sdi
mysql/performance_schema/replication_grou_166.sdi
mysql/performance_schema/rwlock_instances_98.sdi
mysql/performance_schema/session_account__148.sdi
mysql/performance_schema/session_connect__147.sdi
mysql/performance_schema/session_status_177.sdi
mysql/performance_schema/session_variable_180.sdi
mysql/performance_schema/setup_actors_99.sdi
mysql/performance_schema/setup_consumers_100.sdi
mysql/performance_schema/setup_instrument_101.sdi
mysql/performance_schema/setup_objects_102.sdi
mysql/performance_schema/setup_threads_103.sdi
mysql/performance_schema/socket_instances_144.sdi
mysql/performance_schema/socket_summary_b_145.sdi
mysql/performance_schema/socket_summary_b_146.sdi
mysql/performance_schema/status_by_accoun_172.sdi
mysql/performance_schema/status_by_host_173.sdi
mysql/performance_schema/status_by_thread_174.sdi
mysql/performance_schema/status_by_user_175.sdi
mysql/performance_schema/table_handles_155.sdi
mysql/performance_schema/table_io_waits_s_104.sdi
mysql/performance_schema/table_io_waits_s_105.sdi
mysql/performance_schema/table_lock_waits_106.sdi
mysql/performance_schema/threads_107.sdi
mysql/performance_schema/tls_channel_stat_185.sdi
mysql/performance_schema/user_defined_fun_183.sdi
mysql/performance_schema/user_variables_b_171.sdi
mysql/performance_schema/users_141.sdi
mysql/performance_schema/variables_by_thr_178.sdi
mysql/performance_schema/variables_info_181.sdi
mysql/sys/
mysql/sys/sys_config.ibd
sent 169,753,974 bytes received 2,613 bytes 11,707,350.83 bytes/sec
total size is 169,703,438 speedup is 1.00
修改配置文件 vi /etc/my.cnf ?
[mysqld]
#datadir=/var/lib/mysql
datadir=/data/mysql
#socket=/var/lib/mysql/mysql.sock
socket=/data/mysql/mysql.sock
启动MySQL服务器并确认数据目录已更改:
systemctl start mysqld.service
show variables like '%datadir%';
如果出现报错
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details. ?
删掉新的数据目录重新创建,可以删除log底下的所有内容,然后重新再执行初始化mysql就可以了
mysqld? --initialize???--datadir=/data/mysql??--user=mysql
|