安装
一、安装
安装环境
| root@ubuntu:~# lsb_release -ir
Distributor ID: Ubuntu
Release: 18.04
|
1. 下载二进制包
官方下载地址:https://downloads.mysql.com/archives/community/
清华源下载地址:https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/
| root@ubuntu:~# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
root@ubuntu:~# ls
mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
|
2. 解压
| root@ubuntu:~# mkdir /app
root@ubuntu:~# tar zxf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz -C /app/
root@ubuntu:~# mv /app/mysql-5.7.28-linux-glibc2.12-x86_64 /app/mysql
|
3. 添加环境变量
| root@ubuntu:~# echo "export PATH=/app/mysql/bin:$PATH" >> /etc/profile
root@ubuntu:~# source /etc/profile
|
4. 创建mysql
用户
| root@ubuntu:~# groupadd mysql
root@ubuntu:~# useradd -r -g mysql -s /bin/false mysql
|
5. 创建数据目录并授权
| root@ubuntu:~# mkdir -p /data/mysql
root@ubuntu:~# chown -R mysql:mysql /app/mysql/
root@ubuntu:~# chown -R mysql:mysql /data/mysql/
|
6. 初始化数据库
| root@ubuntu:/app/mysql# mysqld --initialize --user=mysql --basedir=/app/mysql --datadir=/data/mysql
2020-12-07T13:18:00.977363Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-12-07T13:18:01.354479Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-12-07T13:18:01.440633Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-12-07T13:18:01.517152Z 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: a1c032f1-388e-11eb-8df1-000c2925b751.
2020-12-07T13:18:01.518560Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-12-07T13:18:04.839325Z 0 [Warning] CA certificate ca.pem is self signed.
2020-12-07T13:18:05.874593Z 1 [Note] A temporary password is generated for root@localhost: /D+(Gsb<w3hH
|
生成的数据目录结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | root@ubuntu:/data/mysql# tree -LF 1
.
├── auto.cnf
├── ca-key.pem
├── ca.pem
├── client-cert.pem
├── client-key.pem
├── ib_buffer_pool
├── ibdata1
├── ib_logfile0
├── ib_logfile1
├── mysql/
├── performance_schema/
├── private_key.pem
├── public_key.pem
├── server-cert.pem
├── server-key.pem
└── sys/
3 directories, 13 files
|
7. 安装出现的错误
| root@ubuntu:/app/mysql# mysqld --initialize --user=mysql --basedir=/app/mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or director
# 解决方法
root@ubuntu:/app/mysql# apt install -y libaio-dev
|
8. 配置文件
1
2
3
4
5
6
7
8
9
10
11
12 | root@gp:~# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/app/mysql # mysql软件所在位置
datadir=/data/mysql # mysql数据存放路径
server_id=1 # 1-65535
port=3306
log_error=/var/log/mysql/mysql.err # 错误日志文件位置,注意给目录赋权
socket=/tmp/mysql.sock # 套接字文件位置
[mysql]
socket=/tmp/mysql.sock
prompt=mysql [\\d]> # 连接到mysql时显示的名称'3306 [(none)]>'
|
9. 启动与停止
9.1 启动
| root@ubuntu:~# mkdir /var/log/mysql
root@ubuntu:~# chown mysql:mysql /var/log/mysql
root@ubuntu:~# /app/mysql/support-files/mysql.server start
Starting MySQL
.Logging to '/var/log/mysql/mysql.err'.
*
root@ubuntu:~# ps aux | grep mysql
root 13521 0.1 0.0 4636 1820 pts/0 S 21:22 0:00 /bin/sh /app/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/ubuntu.pid
mysql 13699 2.4 9.0 1324608 182216 pts/0 Sl 21:22 0:00 /app/mysql/bin/mysqld --basedir=/app/mysql --datadir=/data/mysql --plugin-dir=/app/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql/mysql.err --pid-file=/data/mysql/ubuntu.pid --socket=/tmp/mysql.sock --port=3306
root 13729 0.0 0.0 13144 1076 pts/0 S+ 21:23 0:00 grep --color=auto mysql
|
9.2 停止
| root@ubuntu:~# /app/mysql/support-files/mysql.server stop
Shutting down MySQL
.. *
|
10. 修改初始密码
| root@ubuntu:~# mysqladmin -u root -p password
Enter password: # 输入初始化时日志中的密码/D+(Gsb<w3hH
New password: # 输入新密码
Confirm new password: # 再次输入新密码
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
|
11. MySQL5.7的特点
引入了新的安全机制
- 初始化完成后会生成临时密码
- 密码复杂度:长度超过12位
- 密码过期时间180天
二、MySQL连接
1. socket连接
| # 默认使用socket连接 -S /tmp/mysql.sock可以不加
root@ubuntu:~# mysql -uroot -p -S /tmp/mysql.sock
|
2. TCP/IP连接
| root@ubuntu:~# mysql -h 10.0.0.131 -P 3306 -uroot -p
|
3. 免交互执行SQL语句
| root@ubuntu:~# mysql -u root -proot -e "show databases;"
|
三、初始化配置
1. 初始化配置的方法
- 预编译
- 配置文件
- 命令行(仅限于mysqld和mysqld_safe)
2. 初始化配置文件
配置文件读取顺序
| root@ubuntu:~#:~# mysqld --help --verbose | grep my.cnf
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default
|
如果--defaults-file指定了配置文件,以上文件都不会被读取。
配置文件格式:
| [标签]
配置项=值
# 标签类型:服务端、客户端
# 服务端标签:[mysqld]、[mysqld_safe]、[server]
# 客户端标签:[mysql]、[mysqldump]、[client]
|