MySQL 可以从 YUM 源直接安装 rpm 包,但是这样的定制化程度低,不利于后期维护升级。因此,今天记录一种更灵活的二进制安装方式。参考 MySQL 5.7 官方文档 2.2 章节,具体做法和官方文档略有出入。
1、配置防火墙规则
firewall-cmd --permanent --add-rich-rule=='rule family="ipv4" source address="192.168.27.0/24" service name="mysql" accept'
firewall-cmd --reload
firewall-cmd --list-all
2、创建用户,mysql不需要登录
useradd -s /sbin/nologin mysql
3、规划目录,修改权限
mkdir -p /mysql/{data,run,tmp,log/{iblog,binlog,errlog,slowlog}}
chown -R mysql:mysql /mysql/
tree /mysql/
/mysql/
├── data
├── log
│ ├── binlog
│ ├── errlog
│ ├── iblog
│ └── slowlog
├── run
└── tmp
4、上传安装包,解压缩,修改权限,创建软连接使得 basedir 保持为 /usr/local/mysql
cd /usr/local/
tar xvzf /data/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
chown -R mysql:mysql mysql-5.7.21-linux-glibc2.12-x86_64/
ln -s /usr/local/mysql-5.7.21-linux-glibc2.12-x86_64/ mysql
tree /usr/local/ -L 1
/usr/local/
├── bin
├── etc
├── games
├── include
├── lib
├── lib64
├── libexec
├── mysql -> /usr/local/mysql-5.7.21-linux-glibc2.12-x86_64/
├── mysql-5.7.21-linux-glibc2.12-x86_64
├── sbin
├── share
└── src
5、创建并修改配置文件
vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql # 介质目录
datadir = /mysql/data # 数据目录
port = 3306 # 端口
pid-file = /mysql/mysql.pid # 进程id
user = mysql # 启动用户
socket = /mysql/run/mysql.sock # sock文件地址
server-id = 1 # 用于复制环境钟标识实例,这个在复制环境里唯一
character-set-server = utf8 # 服务端默认字符集,很重要,错误设置会出现乱码
max_connections = 1000 # 允许客户端并发连接的最大数量
max_connect_errors = 6000 # 如果客户端尝试连接的错误数量超过这个参数设置的值,则服务器不再接受新的客户端连接。
open_files_limit = 65535 # 操作系统允许MySQL服务打开的文件数量。
table_open_cache = 128 # 所有线程能打开的表的数量
max_allowed_packet = 4M # 网络传输时单个数据包的大小。
log_bin = /mysql/log/binlog/mysql-bin # bin log目录
binlog_format = row
expire_logs_days = 30
log_error = /mysql/log/errlog/mysql-error.log # error log目录
innodb_log_group_home_dir = /mysql/log/iblog # redo log目录
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /mysql/log/slowlog/mysql-slow.log # slow query log目录
default_storage_engine = InnoDB # 默认存储引擎
interactive_timeout = 28800
wait_timeout = 28800
[client]
port=3306
socket=/mysql/run/mysql.sock
[mysql]
socket=/mysql/run/mysql.sock
6、配置环境变量
cd
vim .bashrc
PATH=$PATH:/usr/local/mysql/bin
export PATH
source .bashrc
7、初始化数据库,目录 /mysql 就生成了初始化的数据。数据库超级管理员 root 的初始密码,从error log里面查看
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/mysql/data
[root@mydb ~]# grep password /mysql/log/errlog/mysql-error.log
2021-09-26T13:33:30.723876Z 1 [Note] A temporary password is generated for root@localhost: X94C5Slafy<o
8、创建 /etc/init.d/mysqld,编辑其中 basedir 和 datadir,启动数据库
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/mysql/data
service mysqld start
9、设置开机自动启动
chkconfig
chkconfig mysqld on
chkconfig
10、登录,操作前要修改root密码
mysql -uroot -p
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>
mysql> alter user 'root'@'localhost' identified by '1qaz@WSX';
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)