文档课题:centos 7.9安装mysql 8.0.11
系统:centos 7.9 64位
数据库:mysql 8.0.11
1、安装
1.1、软件包处理
上传mysql软件包.
# mkdir -p /u01/setup/db
sftp> cd /u01/setup/db
sftp> lcd F:\installmedium\mysql
sftp> put mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz
解压安装包
# cd /u01/setup/db
# tar -zxf mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz
重命名
# mv mysql-8.0.11-linux-glibc2.12-x86_64 /usr/local/mysql
# cd /usr/local/mysql
# ll
total 308
drwxr-xr-x. 2 root root 4096 Oct 20 20:50 bin
drwxr-xr-x. 2 root root 55 Oct 20 20:50 docs
drwxr-xr-x. 3 root root 266 Oct 20 20:50 include
drwxr-xr-x. 5 root root 272 Oct 20 20:50 lib
-rw-r--r--. 1 7161 31415 301518 Apr 8 2018 LICENSE
drwxr-xr-x. 4 root root 30 Oct 20 20:50 man
-rw-r--r--. 1 7161 31415 687 Apr 8 2018 README
drwxr-xr-x. 28 root root 4096 Oct 20 20:50 share
drwxr-xr-x. 2 root root 90 Oct 20 20:50 support-files
1.2、创建目录、用户、组
创建data目录.
# mkdir -p /usr/local/mysql/data
创建组、用户.
# groupadd mysql
# useradd -g mysql mysql
# chown -R mysql.mysql /usr/local/mysql/
1.3、关闭selinux
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
说明:重启系统生效.
1.4、配置Yum源
# mount /dev/sr0 /mnt
# mkdir -p /etc/yum.repos.d/bak
# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
# cat <<EOF>>/etc/yum.repos.d/centos7.repo
[centos7]
name=centos7
baseurl=file:///mnt
enable=1
gpgcheck=0
EOF
# yum makecache
1.5、安装依赖包
# yum -y install numactl
1.6、初始化基础信息
# cd /usr/local/mysql/bin
# ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --initialize
2022-10-20T12:59:47.761766Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2022-10-20T12:59:47.761905Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.11) initializing of server in progress as process 9821
2022-10-20T12:59:50.050581Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: j!DGw8%<hxlc
2022-10-20T12:59:50.875163Z 0 [System] [MY-013170] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.11) initializing of server has completed
说明:后面会用到j!DGw8%<hxlc密码.
1.7、my.cnf
编辑my.cnf文件.
# vi /etc/my.cnf
# cat /etc/my.cnf
#[mysqld]
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

#[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /usr/local/mysql/mysql.sock
character-set-server=utf8
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[client]
socket = /usr/local/mysql/mysql.sock
default-character-set=utf8
说明:后面10行为新添加内容,注释掉前面系统默认的配置,否则无法启动mysql,此处曾困扰笔者较长时间.
1.8、添加mysqld
添加mysqld服务到系统.
# cd /usr/local/mysql
# cp -a ./support-files/mysql.server /etc/init.d/mysql
# chmod +x /etc/init.d/mysql
# chkconfig --add mysql
# chkconfig --list mysql

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]'.

mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
1.9、配置全局环境变量
# vi /etc/profile
添加如下:
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
export PATH
# source /etc/profile
1.10、启动mysql
[root@liujun mysql]# service mysql start
Starting MySQL.Logging to '/usr/local/mysql/data/liujun.err'.
SUCCESS!
[root@liujun mysql]# ps -ef|grep mysql
root 2352 1 0 11:49 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/liujun.pid
mysql 2518 2352 8 11:49 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=liujun.err --pid-file=/usr/local/mysql/data/liujun.pid --socket=/usr/local/mysql/mysql.sock --port=3306
root 2561 2119 0 11:49 pts/0 00:00:00 grep --color=auto mysql
2、mysql信息
登录mysql
[root@liujun mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.11

Copyright (c) 2000, 2018, 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.
说明:此处输入此前生成的密码.

修改root密码.
mysql> alter user 'root'@'localhost' identified by 'mysql_4U';
Query OK, 0 rows affected (0.06 sec)

设置可以远程登录.
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)

mysql> update user set host='%' where user='root' limit 1;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

刷新权限.
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> select distinct concat('user: ''',user,'''@''',host,''';') as query from mysql.user;
+---------------------------------------+
| query |
+---------------------------------------+
| user: 'root'@'%'; |
| user: 'mysql.infoschema'@'localhost'; |
| user: 'mysql.session'@'localhost'; |
| user: 'mysql.sys'@'localhost'; |
+---------------------------------------+
4 rows in set (0.00 sec)

查看所有数据库.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

查数据库端口号.
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)

查所有数据大小.
mysql> select concat(round(sum(data_length/1024/1024),2),'MB') as data from information_schema.tables;
+--------+
| data |
+--------+
| 2.14MB |
+--------+
1 row in set (0.14 sec)

数据库字符集.
mysql> show variables like 'character%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

mysql> status
--------------
mysql Ver 8.0.11 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)

Connection id: 8
Current database: mysql
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.11
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8mb4
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /usr/local/mysql/mysql.sock
Uptime: 9 min 50 sec

Threads: 2 Questions: 57 Slow queries: 0 Opens: 271 Flush tables: 2 Open tables: 247 Queries per second avg: 0.096
--------------

查最大连接数.
mysql> show variables like '%max_connections%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| max_connections | 151 |
| mysqlx_max_connections | 100 |
+------------------------+-------+
2 rows in set (0.00 sec)

查当前连接数.
mysql> show status like 'Threads%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_cached | 0 |
| Threads_connected | 1 |
| Threads_created | 1 |
| Threads_running | 2 |
+-------------------+-------+
4 rows in set (0.00 sec)
Threads_cached : 当前线程缓存中的空闲线程
Threads_connected :当前已建立连接的数量,一个连接就要一个线程,所以也可以看成当前被使用的线程数
Threads_created :最近一次服务启动已创建的线程数量
Threads_running :当前激活的线程数,不代表正在使用的线程数,有时连接虽然建立但处于sleep状态,相对应的线程也是sleep状态

查数据文件路径
mysql> show variables like 'datadir';
+---------------+------------------------+
| Variable_name | Value |
+---------------+------------------------+
| datadir | /usr/local/mysql/data/ |
+---------------+------------------------+
1 row in set (0.00 sec)

告警日志路径
mysql> show variables like 'log_error';
+---------------+--------------+
| Variable_name | Value |
+---------------+--------------+
| log_error | ./liujun.err |
+---------------+--------------+
1 row in set (0.00 sec)

本地启动后的运行统计时间(单位:秒)
mysql> show status like 'uptime';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime | 802 |
+---------------+-------+
1 row in set (0.00 sec)
慢查询次数
mysql> show status like 'slow_queries';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries | 0 |
+---------------+-------+
1 row in set (0.00 sec)

慢查询限定时间
mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)

慢查询日志路径
mysql> show variables where variable_name='slow_query_log_file';
+---------------------+---------------------------------------+
| Variable_name | Value |
+---------------------+---------------------------------------+
| slow_query_log_file | /usr/local/mysql/data/liujun-slow.log |
+---------------------+---------------------------------------+
1 row in set (0.00 sec)