MySQL运维实战(1.1)安装部署:使用RPM进行安装部署
我们在生产环境部署mysql时,一般很少使用rpm。
用rpm或或者其他包管理器安装mysql,好处是安装简单,而且很多系统可能都自带了某个版本的mysql。
但是使用RPM安装也存在一些缺点:
1、rpm依赖包的问题。如果删除线上机器的冲突包,可能引起其他软件运行异常。
2、多版本管理不方便。升级mysql或降级mysql不方便。无法多个版本同时运行。
3、单机多实例管理不方便。
但是对于DBA,我们需要掌握如何使用rpm包安装mysql。
因为很可能需要处理用这种方式安装的mysql遇到的一些问题,如数据库无法启动、默认路径空间占满,需要将数据需要换到别的磁盘、调整参数等。
环境
CentOS 7.9
[root@box1 software]# cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core) [root@box1 software]# uname -a Linux box1 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
建议选择下载 RPM Bundle包,里面包含了需要的各个rpm包
解压bundle包,可以看到里面的rpm包,
# tar xf mysql-5.7.32-1.el7.x86_64.rpm-bundle.tar -C rpm/5.7/ # [root@box1 5.7]# ls -lh 总用量 1.1G -rw-r--r--. 1 root root 518M 9月 25 2020 mysql-5.7.32-1.el7.x86_64.rpm-bundle.tar -rw-r--r--. 1 7155 31415 26M 9月 25 2020 mysql-community-client-5.7.32-1.el7.x86_64.rpm -rw-r--r--. 1 7155 31415 308K 9月 25 2020 mysql-community-common-5.7.32-1.el7.x86_64.rpm -rw-r--r--. 1 7155 31415 3.8M 9月 25 2020 mysql-community-devel-5.7.32-1.el7.x86_64.rpm -rw-r--r--. 1 7155 31415 46M 9月 25 2020 mysql-community-embedded-5.7.32-1.el7.x86_64.rpm -rw-r--r--. 1 7155 31415 23M 9月 25 2020 mysql-community-embedded-compat-5.7.32-1.el7.x86_64.rpm -rw-r--r--. 1 7155 31415 125M 9月 25 2020 mysql-community-embedded-devel-5.7.32-1.el7.x86_64.rpm -rw-r--r--. 1 7155 31415 2.4M 9月 25 2020 mysql-community-libs-5.7.32-1.el7.x86_64.rpm -rw-r--r--. 1 7155 31415 1.3M 9月 25 2020 mysql-community-libs-compat-5.7.32-1.el7.x86_64.rpm -rw-r--r--. 1 7155 31415 174M 9月 25 2020 mysql-community-server-5.7.32-1.el7.x86_64.rpm -rw-r--r--. 1 7155 31415 120M 9月 25 2020 mysql-community-test-5.7.32-1.el7.x86_64.rpm
安装rpm包
需要安装mysql-community-server, mysql-community-common, mysql-community-client, mysql-community-libs这些包
[root@box1 5.7]# rpm -ivh mysql-community-client-5.7.32-1.el7.x86_64.rpm mysql-community-common-5.7.32-1.el7.x86_64.rpm mysql-community-server-5.7.32-1.el7.x86_64.rpm mysql-community-libs-5.7.32-1.el7.x86_64.rpm 警告:mysql-community-client-5.7.32-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 错误:依赖检测失败: mariadb-libs 被 mysql-community-libs-5.7.32-1.el7.x86_64 取代
卸载冲突的包
安装报错,因为系统里已经安装了mariadb相关lib,需要先将这些lib卸载
[root@box1 5.7]# rpm -qa | grep maria mariadb-devel-5.5.68-1.el7.x86_64 mariadb-libs-5.5.68-1.el7.x86_64 [root@box1 5.7]# rpm -evf mariadb-devel-5.5.68-1.el7.x86_64 mariadb-libs-5.5.68-1.el7.x86_64 错误:依赖检测失败: libmysqlclient.so.18()(64bit) 被 (已安裝) net-snmp-agent-libs-1:5.7.2-49.el7.x86_64 需要 libmysqlclient.so.18()(64bit) 被 (已安裝) net-snmp-1:5.7.2-49.el7.x86_64 需要 libmysqlclient.so.18()(64bit) 被 (已安裝) postfix-2:2.10.1-9.el7.x86_64 需要 libmysqlclient.so.18()(64bit) 被 (已安裝) qt-mysql-1:4.8.7-8.el7.x86_64 需要 libmysqlclient.so.18()(64bit) 被 (已安裝) qt5-qtbase-mysql-5.9.7-4.el7.x86_64 需要 libmysqlclient.so.18()(64bit) 被 (已安裝) qt3-MySQL-3.3.8b-51.el7.x86_64 需要 libmysqlclient.so.18()(64bit) 被 (已安裝) MySQL-python-1.2.5-1.el7.x86_64 需要 libmysqlclient.so.18(libmysqlclient_18)(64bit) 被 (已安裝) net-snmp-agent-libs-1:5.7.2-49.el7.x86_64 需要 libmysqlclient.so.18(libmysqlclient_18)(64bit) 被 (已安裝) postfix-2:2.10.1-9.el7.x86_64 需要 libmysqlclient.so.18(libmysqlclient_18)(64bit) 被 (已安裝) qt-mysql-1:4.8.7-8.el7.x86_64 需要 libmysqlclient.so.18(libmysqlclient_18)(64bit) 被 (已安裝) qt5-qtbase-mysql-5.9.7-4.el7.x86_64 需要 libmysqlclient.so.18(libmysqlclient_18)(64bit) 被 (已安裝) qt3-MySQL-3.3.8b-51.el7.x86_64 需要 libmysqlclient.so.18(libmysqlclient_18)(64bit) 被 (已安裝) MySQL-python-1.2.5-1.el7.x86_64 需要 mysql-libs 被 (已安裝) net-snmp-1:5.7.2-49.el7.x86_64 需要 ### 加--nodeps强制卸载mariadb lib [root@box1 5.7]# rpm -ev --nodeps mariadb-devel-5.5.68-1.el7.x86_64 mariadb-libs-5.5.68-1.el7.x86_64 软件包准备中... mariadb-devel-1:5.5.68-1.el7.x86_64 mariadb-libs-1:5.5.68-1.el7.x86_64
卸载完mariadb lib包之后重新安装
[root@box1 5.7]# rpm -ivh mysql-community-client-5.7.32-1.el7.x86_64.rpm mysql-community-common-5.7.32-1.el7.x86_64.rpm mysql-community-server-5.7.32-1.el7.x86_64.rpm mysql-community-libs-5.7.32-1.el7.x86_64.rpm 警告:mysql-community-client-5.7.32-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-common-5.7.32-1.e################################# [ 25%] 2:mysql-community-libs-5.7.32-1.el7################################# [ 50%] 3:mysql-community-client-5.7.32-1.e################################# [ 75%] 4:mysql-community-server-5.7.32-1.e################################# [100%]
安装完之后的相关文件
/usr/bin目录下,主要是mysql相关的客户端命令、管理命令
[root@box1 5.7]# ls /usr/bin/mysql* /usr/bin/mysql /usr/bin/mysqlcheck /usr/bin/mysqldump /usr/bin/mysql_install_db /usr/bin/mysql_secure_installation /usr/bin/mysql_ssl_rsa_setup /usr/bin/mysqladmin /usr/bin/mysql_config_editor /usr/bin/mysqldumpslow /usr/bin/mysql_plugin /usr/bin/mysqlshow /usr/bin/mysql_tzinfo_to_sql /usr/bin/mysqlbinlog /usr/bin/mysqld_pre_systemd /usr/bin/mysqlimport /usr/bin/mysqlpump /usr/bin/mysqlslap /usr/bin/mysql_upgrade
/usr/sbin
mysqld,mysql服务器的可执行文件
[root@box1 5.7]# ls /usr/sbin/mysql* /usr/sbin/mysqld /usr/sbin/mysqld-debug
/usr/lib64/mysql
mysql client lib和plugin lib
[root@box1 5.7]# ls /usr/lib64/mysql/ libmysqlclient.so.20 libmysqlclient.so.20.3.19 mecab plugin
/usr/share/mysql
主要是mysql字符集和error msg相关文件
[root@box1 5.7]# ls /usr/share/mysql/ bulgarian dictionary.txt estonian greek italian mysql-log-rotate mysql_system_tables.sql polish serbian ukrainian charsets dutch fill_help_tables.sql hungarian japanese mysql_security_commands.sql mysql_test_data_timezone.sql portuguese slovak uninstall_rewriter.sql czech english french innodb_memcached_config.sql korean mysql_sys_schema.sql norwegian romanian spanish danish errmsg-utf8.txt german install_rewriter.sql magic mysql_system_tables_data.sql norwegian-ny russian swedish
/etc/my.cnf
配置文件
/var/lib/mysql
mysql数据文件
[root@box1 5.7]# ls /var/lib/mysql* /var/lib/mysql: /var/lib/mysql-files: /var/lib/mysql-keyring:
启动脚本
服务启动文件 | /etc/systemd/system/multi-user.target.wants/mysqld.service |
数据库初始化脚本 | /usr/bin/mysqld_pre_systemd |
启动mysql
使用service 启动mysql实例
[root@box1 5.7]# service mysqld start Redirecting to /bin/systemctl start mysqld.service
[root@box1 5.7]# ps -elf | grep mysqld 1 S mysql 12663 1 0 80 0 - 280540 poll_s 08:17 ? 00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid 0 S root 12959 2276 0 80 0 - 28206 pipe_w 08:21 pts/0 00:00:00 grep --color=auto mysqld
登陆mysql
第一次安装后,会生产mysql root账号的默认的临时密码,可以在/var/log/mysql.log中找到。
用临时密码登陆后,需要修改密码才能进行操作
[root@box1 5.7]# grep -i pass /var/log/mysqld.log 2021-03-31T12:14:47.169374Z 1 [Note] A temporary password is generated for root@localhost: _0eaf&hyQ9Ha 2021-03-31T12:23:26.373529Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO) [root@box1 5.7]# mysql -uroot -h127.0.0.1 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.32 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> select 1; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> alter user 'root'@'localhost' identified by 'helloWorld#'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql> alter user 'root'@'localhost' identified by 'helloWorld#123'; Query OK, 0 rows affected (0.00 sec) mysql> select now(); +---------------------+ | now() | +---------------------+ | 2021-03-31 08:27:57 | +---------------------+ 1 row in set (0.00 sec)
关于mysql初始化
使用service启动mysql时,初次使用mysql,会执行脚本/usr/bin/mysqld_pre_systemd进行初始化,
脚本里进行了几个重要操作
往mysql.plugin里添加validate_password插件
使用mysqld --initialize初始化mysql系统数据库
使用mysql_ssl_rsa_setup创建ssl证书文件
# more /usr/bin/mysqld_pre_systemd ... install_validate_password_sql_file () { local initfile initfile="$(mktemp /var/lib/mysql-files/install-validate-password-plugin.XXXXXX.sql)" chmod a+r "$initfile" echo "SET @@SESSION.SQL_LOG_BIN=0;" > "$initfile" echo "INSERT INTO mysql.plugin (name, dl) VALUES ('validate_password', 'validate_password.so');" >> $initfile echo $initfile } ... # If special mysql dir is in place, skip db install [ -d "$datadir/mysql" ] && exit 0 # Create initial db and install validate_password plugin initfile="$(install_validate_password_sql_file)" /usr/sbin/mysqld ${instance:+--defaults-group-suffix=@$instance} --initialize \ --datadir="$datadir" --user=mysql --init-file="$initfile" rm -f "$initfile" # Generate certs if needed if [ -x /usr/bin/mysql_ssl_rsa_setup -a ! -e "${datadir}/server-key.pem" ] ; then /usr/bin/mysql_ssl_rsa_setup --datadir="$datadir" --uid=mysql >/dev/null 2>&1 fi