MySQL运维实战(1.2)安装部署:使用二进制安装部署
一般在生产环境,我们会使用二进制安装的方式安装MySQL。
使用二进制安装,在处理单机多实例、升级MySQL等场景下更加方便。如果有特殊的需求(比如要打一些patch),我们还可以自己编译二进制。
1、下载二进制文件
根据操作系统版本,下载二进制包
下载其中的Compressed TAR,如:
Compressed TAR Archive | 8.0.32 | 495.7M | |
(mysql-8.0.32-el7-x86_64.tar.gz) |
2、解压
将下载的二进制文件下载到某个目录。
一般我们会将mysql二进制文件放到/usr/local/mysql 或者 /opt/mysql。
tar xvf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz -C /opt mv mysql-8.0.32-linux-glibc2.12-x86_64 mysql
ls /opt/mysql/ bin docs include lib LICENSE man README share support-files
3、准备配置文件
我们规划将MySQL数据库文件放在/data/mysql01路径下。
mysql数据目录: /data/mysql01/
配置文件:/data/mysql01/my.cnf
[mysqld] #dir basedir=/opt/mysql lc_messages_dir=/opt/mysql/share datadir=/data/mysql01/data tmpdir=/data/mysql01/tmp log-error=/data/mysql01/log/alert.log slow_query_log_file=/data/mysql01/log/slow.log general_log_file=/data/mysql01/log/general.log socket=/data/mysql01/run/mysql.sock #innodb innodb_data_home_dir=/data/mysql01/data innodb_log_group_home_dir=/data/mysql01/data innodb_data_file_path=ibdata1:128M:autoextend
配置文件核心内容是[mysqld]下的相关路径配置:
basedir: mysql二进制文件路径
datadir: mysql数据目录
这里只配置了启动MySQL需要的最基本的参数。
真实环境中,需要根据业务需求和服务器配置优化配置,后续单独讲。
4、初始化数据库
-- 创建mysql用户 groupadd mysql useradd mysql -g mysql -- 创建相关目录 mkdir -p /data/mysql01/{data,binlog,log,run,tmp} chown -R mysql:mysql /data/mysql01 -- 初始化数据库 ./bin/mysqld --defaults-file=/data/mysql01/my.cnf --initialize --user=mysql
初始化完成后,查看alert.log中的临时密码
tail /data/mysql01/log/alert.log 2021-03-29T11:20:45.693865Z 0 [Warning] InnoDB: New log files created, LSN=45790 2021-03-29T11:20:45.789596Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2021-03-29T11:20:45.865865Z 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: ce7101f3-9080-11eb-9d26-080027475f71. 2021-03-29T11:20:45.869311Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2021-03-29T11:20:46.234753Z 0 [Warning] CA certificate ca.pem is self signed. 2021-03-29T11:20:46.286920Z 1 [Note] A temporary password is generated for root@localhost: kguRrCbXw9;
5、启动实例
mysql服务器进程是mysqld, 可以通过执行mysqld命令直接启动数据库,当然通常我们会使用一些封装过的脚本来启动mysql。
mysqld_safe就是一个常用的脚本,它能启动mysqld,并在mysqld异常退出后重新启动mysqld
[root@box1 ~]# ./bin/mysqld_safe --defaults-file=/data/mysql01/my.cnf & [1] 27896 [root@box1 ~]# 2021-03-29T11:24:31.202499Z mysqld_safe Logging to '/data/mysql01/log/alert.log'.
通过ps,可以看到mysqld进程,以及命令行参数,mysqld_safe是mysqld的父进程。
[root@box1 mysql]# ps -elf | grep mysqld 4 S root 6445 2276 0 80 0 - 28354 do_wai 05:20 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql01/my.cnf 4 S mysql 6738 6445 0 80 0 - 518211 poll_s 05:20 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql01/my.cnf --basedir=/usr/local --datadir=/data/mysql01/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql01/log/alert.log --pid-file=box1.pid --socket=/data/mysql01/run/mysql.sock 0 S root 6784 2276 0 80 0 - 28206 pipe_w 05:21 pts/0 00:00:00 grep --color=auto mysqld
可以看到mysqld命令行的关键参数
--defaults-file: 启动参数文件
--basedir: mysql软件的basedir
--datadir: 数据目录
--plugin-dir: mysql插件lib库路径
--user: 运行mysql的OS账号
--log-error: 错误日志路径
--socket: socket连接文件
如果不提供这些参数,mysqld会使用编译时的默认参数。
默认参数:
[root@box1 mysql]# ./bin/mysqld --print-defaults ./bin/mysqld would have been started with the following arguments: --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --symbolic-links=0
默认参数文件路径:
[root@box1 mysql]# ./bin/mysqld --verbose --help | more 2021-03-31T09:33:30.820738Z 0 [ERROR] COLLATION 'latin1_swedish_ci' is not valid for CHARACTER SET 'utf8mb4' ./bin/mysqld Ver 5.7.32-debug for Linux on x86_64 (lazybug) 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. Starts the MySQL database server. Usage: ./bin/mysqld [OPTIONS] Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf The following groups are read: mysqld server mysqld-5.7 The following options may be given as the first argument: --print-defaults Print the program argument list and exit. --no-defaults Don't read default options from any option file, except for login file. --defaults-file=# Only read default options from the given file #.
通过mysqld --verbose --help可以看到参数文件搜索路径:
/etc/my.cnf, /etc/mysql/my.cnf, /usr/local/mysql/etc/my.cnf, ~/.my.cnf
在运维多实例mysql时,我们通常会指定my.cnf的路径,而不使用默认路径的配置文件。
避免读取默认配置参数文件中的配置,引起各种问题。
加了--defaults-file后,就只会从defaults-file指定文件中读取配置。
日志文件
如果启动过程中有问题,可以通过alert日志文件查看具体的问题
$ tail -100 /data/mysql01/log/alert.log 2021-03-29T11:24:31.229330Z mysqld_safe Starting mysqld daemon with databases from /data/mysql01/data 2021-03-29T11:24:31.407665Z 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.7.32-debug) starting as process 28189 ... 2021-03-29T11:24:31.412908Z 0 [Note] InnoDB: PUNCH HOLE support available 2021-03-29T11:24:31.412934Z 0 [Note] InnoDB: !!!!!!!! UNIV_DEBUG switched on !!!!!!!!! 2021-03-29T11:24:31.412939Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2021-03-29T11:24:31.412942Z 0 [Note] InnoDB: Uses event mutexes 2021-03-29T11:24:31.412947Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier 2021-03-29T11:24:31.412950Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11 2021-03-29T11:24:31.412954Z 0 [Note] InnoDB: Using Linux native AIO 2021-03-29T11:24:31.413276Z 0 [Note] InnoDB: Number of pools: 1 2021-03-29T11:24:31.413391Z 0 [Note] InnoDB: Using CPU crc32 instructions 2021-03-29T11:24:31.414823Z 0 [Note] InnoDB: Initializing buffer pool, total size = 512M, instances = 1, chunk size = 128M 2021-03-29T11:24:31.589281Z 0 [Note] InnoDB: Completed initialization of buffer pool 2021-03-29T11:24:31.594759Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). 2021-03-29T11:24:31.611389Z 0 [Note] InnoDB: Highest supported file format is Barracuda. 2021-03-29T11:24:31.667674Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables 2021-03-29T11:24:31.667861Z 0 [Note] InnoDB: Setting file '/data/mysql01/data/ibtmp1' size to 12 MB. Physically writing the file full; Please wait ... 2021-03-29T11:24:31.680116Z 0 [Note] InnoDB: File '/data/mysql01/data/ibtmp1' size is now 12 MB. 2021-03-29T11:24:31.685528Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active. 2021-03-29T11:24:31.685540Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active. 2021-03-29T11:24:31.689004Z 0 [Note] InnoDB: Waiting for purge to start 2021-03-29T11:24:31.741474Z 0 [Note] InnoDB: 5.7.32 started; log sequence number 2746702 2021-03-29T11:24:31.742643Z 0 [Note] InnoDB: Loading buffer pool(s) from /data/mysql01/data/ib_buffer_pool 2021-03-29T11:24:31.742725Z 0 [Note] Plugin 'FEDERATED' is disabled. 2021-03-29T11:24:31.752433Z 0 [Note] InnoDB: Buffer pool(s) load completed at 210329 7:24:31 2021-03-29T11:24:31.757409Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them. 2021-03-29T11:24:31.757426Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory. 2021-03-29T11:24:31.758133Z 0 [Warning] CA certificate ca.pem is self signed. 2021-03-29T11:24:31.758185Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory. 2021-03-29T11:24:31.758504Z 0 [Note] Server hostname (bind-address): '*'; port: 3306 2021-03-29T11:24:31.759379Z 0 [Note] IPv6 is available. 2021-03-29T11:24:31.759408Z 0 [Note] - '::' resolves to '::'; 2021-03-29T11:24:31.759434Z 0 [Note] Server socket created on IP: '::'. 2021-03-29T11:24:31.796451Z 0 [Note] Event Scheduler: Loaded 0 events 2021-03-29T11:24:31.796935Z 0 [Note] /usr/local/mysql/bin/mysqld: ready for connections. Version: '5.7.32-debug' socket: '/data/mysql01/run/mysql.sock' port: 3306 lazybug
日志文件最后一行显示启动成功,提示本地socket文件路径和监听端口。
6、修改默认密码
mysql 5.7开始,需要先修改密码,才能正常访问数据库:
[root@box1 run]# /usr/local/mysql/bin/mysql -uroot -h127.0.0.1 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.32-debug 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'; Query OK, 0 rows affected (0.00 sec) mysql> select 1; +---+ | 1 | +---+ | 1 | +---+ 1 row in set (0.00 sec)