零 修订记录
序号 | 修订内容 | 修订时间 |
---|---|---|
1 | 新增 | 2021/2/21 |
一 摘要
本文主要介绍基于mysql 安装包 绿色安装mysql
二 环境信息
(一)操作系统
centos 7.3.1611
(二)数据库版本
mysql:5.7.28
mysql 官网下载
三 实施
(一)绿色安装mysql
3.1.1 卸载mysql 和 mariadb
[root@jichu ~]# rpm -qa | grep mysql
[root@jichu ~]#
[root@jichu ~]# rpm -qa | grep maria
mariadb-libs-5.5.52-1.el7.x86_64
[root@jichu ~]#
需卸载 mariadb,我这里把她强制卸载了。
[root@jichu ~]# rpm -e mariadb-libs-5.5.52-1.el7.x86_64
error: Failed dependencies:
libmysqlclient.so.18()(64bit) is needed by (installed) postfix-2:2.10.1-6.el7.x86_64
libmysqlclient.so.18(libmysqlclient_18)(64bit) is needed by (installed) postfix-2:2.10.1-6.el7.x86_64
[root@jichu ~]# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64
[root@jichu ~]#
3.1.2 解压mysql 到/usr/local
[root@jichu local]# pwd
/usr/local
[root@jichu local]# tar -zxvf /home/yunwei/software/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
[root@jichu local]# mv mysql-5.7.28-linux-glibc2.12-x86_64/ mysql
3.1.3 创建数据库文件目录和日志目录
[root@jichu local]# mkdir /usr/local/mysql/{data,logs}
[root@jichu local]# ll mysql/
total 292
drwxr-xr-x. 2 root root 4096 Mar 16 09:25 bin
drwxr-xr-x. 2 root root 6 Mar 16 09:30 data
drwxr-xr-x. 2 root root 55 Mar 16 09:25 docs
drwxr-xr-x. 3 root root 4096 Mar 16 09:25 include
drwxr-xr-x. 5 root root 230 Mar 16 09:25 lib
-rw-r--r--. 1 7161 31415 279547 Sep 27 15:00 LICENSE
drwxr-xr-x. 2 root root 6 Mar 16 09:30 logs
drwxr-xr-x. 4 root root 30 Mar 16 09:25 man
-rw-r--r--. 1 7161 31415 587 Sep 27 15:00 README
drwxr-xr-x. 28 root root 4096 Mar 16 09:25 share
drwxr-xr-x. 2 root root 90 Mar 16 09:25 support-files
[root@jichu local]#
3.1.4 新建空的mysql.log 文件(否则后期启动会报错)
[root@jichu local]# cd mysql/logs/
[root@jichu logs]# ll
total 0
[root@jichu logs]# echo "" > mysql.log
[root@jichu logs]# ll
total 4
-rw-r--r--. 1 root root 1 Mar 16 09:38 mysql.log
[root@jichu logs]#
3.1.5 新建mysql 用户及用户组,以及修改mysql 目录权限
[root@jichu logs]# groupadd mysql
[root@jichu logs]# useradd -r -g mysql mysql
[root@jichu logs]# chown -R mysql:mysql /usr/local/mysql
[root@jichu logs]#
3.1.6 初始化mysql 并获取初始root 密码
[root@jichu logs]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2020-03-16T02:31:58.112913Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-03-16T02:31:58.834197Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-03-16T02:31:58.941939Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-03-16T02:31:59.003728Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 4f9c8a4d-672e-11ea-a3eb-000c29076cf3.
2020-03-16T02:31:59.004790Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2020-03-16T02:31:59.569039Z 0 [Warning] CA certificate ca.pem is self signed.
2020-03-16T02:31:59.987030Z 1 [Note] A temporary password is generated for root@localhost: h7T8lyDA<*qW
[root@jichu logs]#
初始密码是:h7T8lyDA<*qW 要记下来。
3.1.6 配置开机启动
[root@jichu logs]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@jichu logs]# chmod u+x /etc/init.d/mysqld
[root@jichu logs]# chkconfig --add mysqld
[root@jichu logs]# chkconfig mysqld on
== 检查下
[root@jichu logs]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use ‘systemctl list-unit-files‘.
To see services enabled on particular target use
‘systemctl list-dependencies [target]‘.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@jichu logs]#
3.1.7添加配置文件 /etc/my.cnf
我的配置文件如下,大的目录要保持一样,小配置 大家根据自己需要增减。
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[client]
default-character-set=utf8
socket=/usr/local/mysql/data/mysql.sock
[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
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/usr/local/mysql/logs/mysql.log
#pid-file=/var/run/mysqld/mysqld.pid
pid-file=/usr/local/mysql/mysqld.pid
lower_case_table_names=0
sql_mode=‘STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION‘
character-set-server=utf8
collation-server=utf8_bin
default-storage-engine=INNODB
max_allowed_packet=256M
transaction-isolation=READ-COMMITTED
log-bin=mysql-bin
server-id=1
innodb_large_prefix=ON
innodb_file_format=Barracuda
innodb_log_file_size=2G
3.1.8 修改root 默认密码
启动mysql,然后用初始化的root 密码 登录到 控制台,然后修改root 密码,添加root 远程访问权限,修改root 密码 永不过期。
[root@jichu etc]# service mysqld start
Starting MySQL.......... SUCCESS!
[root@jichu etc]# /usr/local/mysql/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28-log
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> alter user ‘root‘@‘localhost‘ identified by ‘root‘;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> ALTER USER ‘root‘@‘localhost‘ PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.00 sec)
mysql>
相关sql:
alter user ‘root‘@‘localhost‘ identified by ‘root‘;
GRANT ALL PRIVILEGES ON . TO root@"%" IDENTIFIED BY "root";
ALTER USER ‘root‘@‘localhost‘ PASSWORD EXPIRE NEVER;
3.1.9 重启mysql ,用新密码登录验证
[root@jichu etc]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@jichu etc]# /usr/local/mysql/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.28-log 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>