二进制格式安装MySQL
下载安装包
- /usr/src中下载二进制格式的 mysql 软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
- 创建用户和组
[root@localhost src]# groupadd -r mysql
[root@localhost src]# useradd -M -r -s /sbin/nologin -g mysql mysql
- 解压软件至 /usr/local/
[root@localhost src]# tar zxvf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
顺便做个软链接
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql-5.7.34-linux-glibc2.12-x86_64 sbin share src
[root@localhost local]# ln -s mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 4月 10 2018 bin
drwxr-xr-x. 2 root root 6 4月 10 2018 etc
drwxr-xr-x. 2 root root 6 4月 10 2018 games
drwxr-xr-x. 2 root root 6 4月 10 2018 include
drwxr-xr-x. 2 root root 6 4月 10 2018 lib
drwxr-xr-x. 2 root root 6 4月 10 2018 lib64
drwxr-xr-x. 2 root root 6 4月 10 2018 libexec
lrwxrwxrwx. 1 root root 36 8月 25 00:49 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 8月 25 00:47 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 4月 10 2018 sbin
drwxr-xr-x. 5 root root 49 7月 13 08:02 share
drwxr-xr-x. 2 root root 6 4月 10 2018 src
- 修改目录 /usr/local/mysql 的属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql*
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 4月 10 2018 bin
drwxr-xr-x. 2 root root 6 4月 10 2018 etc
drwxr-xr-x. 2 root root 6 4月 10 2018 games
drwxr-xr-x. 2 root root 6 4月 10 2018 include
drwxr-xr-x. 2 root root 6 4月 10 2018 lib
drwxr-xr-x. 2 root root 6 4月 10 2018 lib64
drwxr-xr-x. 2 root root 6 4月 10 2018 libexec
lrwxrwxrwx. 1 mysql mysql 36 8月 25 00:49 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 8月 25 00:47 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 4月 10 2018 sbin
drwxr-xr-x. 5 root root 49 7月 13 08:02 share
drwxr-xr-x. 2 root root 6 4月 10 2018 src
- 添加环境变量
[root@localhost local]# vim /etc/profile.d/mysql.sh
[root@localhost local]# cat /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost local]# source /etc/profile.d/mysql.sh
[root@localhost local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
- 建立数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll -d /opt/data/
drwxr-xr-x. 2 mysql mysql 6 8月 25 00:56 /opt/data/
- 初始化数据库
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data --basedir=/usr/local/mysql
2021-08-25T07:58:24.328868Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use -
-explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-25T07:58:24.543890Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-25T07:58:24.592922Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-25T07:58:24.658072Z 0 [Warning] No existing UUID has been found, so we assume that this is the fi
rst time that this server has been started. Generating a new UUID: 394478b8-057a-11ec-962c-000c2933c088.
2021-08-25T07:58:24.658830Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' c
annot be opened.
2021-08-25T07:58:25.077346Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-25T07:58:25.408988Z 1 [Note] A temporary password is generated for root@localhost: Zd,<)UDF.9ql
[root@localhost ~]# echo 'Zd,<)UDF.9ql' > sqlkey.txt ///备份数据库临时密码
[root@localhost ~]# ls
anaconda-ks.cfg Desktop Documents Downloads Music original-ks.cfg Pictures Public sqlkey.txt Templates Videos
- 修改配置文件(没有就创一个)
[root@localhost ~]# cp /etc/my.cnf /etc/my.cnf.back ##备份配置文件
[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
datadir=/opt/data
socket=/tmp/mysql.sock
basedir=/usr/local/mysql
port=3306
character-set-server=utf8
log-error=/var/log/mysqld.log
pid-file=/opt/data/mysql.pid
配置参数解析
配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效
/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf
参数 | 说明 |
---|---|
port = 3306 | 设置监听端口 |
socket = /tmp/mysql.sock | 指定套接字文件位置 |
basedir = /usr/local/mysql | 指定MySQL的安装路径 |
datadir = /data/mysql | 指定MySQL的数据存放路径 |
pid-file = /data/mysql/mysql.pid | 指定进程ID文件存放路径 |
user = mysql | 指定MySQL以什么用户的身份提供服务 |
skip-name-resolve | 禁止MySQL对外部连接进行DNS解析使用这一选项可以消除MySQL进行DNS解析的时间。若开启该选项,则所有远程主机连接授权都要使用IP地址方式否则MySQL将无法正常处理连接请求 |
- 配置服务启动脚本
[root@localhost ~]# cd /usr/local/mysql/support-files
[root@localhost support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@localhost support-files]# cp -a mysql.server /etc/init.d/mysqld
[root@localhost support-files]# vim /etc/init.d/mysqld
........
basedir=/usr/local/mysql #修改这两行参数
datadir=/opt/data
........
- 启动mysql
[root@localhost ~]# service mysqld start
Starting MySQL. SUCCESS!
- 设置开机自动启动
[root@localhost ~]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@localhost ~]# chkconfig --add mysqld ##添加开机自启
[root@localhost ~]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
12 . 修改密码
[root@localhost ~]# mysql -uroot -p'Zd,<)UDF.9ql' ## 用单引号括起密钥
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> set password=password('1');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>