ubuntu 源码安装 mysql8
19-05-19 07:40
720
# Mysql安装
## 基础软件包安装
- `sudo apt-get install cmake`
- `sudo apt-get install bison`
- `sudo apt-get install libncurses5-dev`
- boost安装:[下载](https://dl.bintray.com/boostorg/release/1.69.0/source/boost_1_69_0.tar.gz) 或 [网盘下载](https://pan.baidu.com/s/1sYkUdUajt6nwxGJ51jZTwA) 提取码:3xnw
解压包: `sudo tar -zxvf boost_1_69_0.tar.gz -C /home/loso/src/`
执行安装:`sudo ./bootstrap.sh` && `sudo ./b2 install`
## Mysql server安装
- mysql[官网下载](https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz) 或 [网盘下载](https://pan.baidu.com/s/1G1VIgEveyPXUNN-JKADA2Q) 提取码:6gn3
- mysql解压: `sudo xz -d mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz ` `sudo tar -xvf mysql-8.0.15-linux-glibc2.12-x86_64.tar -C /home/loso/src/`
- `sudo cp -r /home/loso/src/mysql-8.0.15-linux-glibc2.12-x86_64 /usr/local/mysql`
- 创建用户:
`sudo groupadd mysql`
`sudo useradd -r -g mysql mysql`
`sudo chown mysql.mysql /usr/local/mysql -R`
- 创建数据目录:
`mkdir -p /usr/local/mysql/data ` && ` chown mysql.mysql -R /usr/local/mysql/data `
- 执行安装:
`cd /usr/local/mysql`
`./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data`
- 问题:libaio.so.1: cannot open shared object file `sudo apt-get install libaio1`
- 配置文件: `sudo vim /etc/mysql/my.cnf`
```mysql
[mysqld] #服务端基本设置
port = 3306 #MySQL监听端口
basedir=/usr/local/mysql #使用该目录作为根目录(安装目录)
datadir = /usr/local/mysql/data #数据文件存放的目录
socket=/tmp/mysql.sock #mysql:mysql所属(默认)为MySQL客户端程序和服务器之间的本
地通讯指定一个套接字文件
user=mysql #MySQL启动用户
character_set_server = utf8mb4 #服务端默认编码(数据库级别)
collation_server = utf8mb4_bin #服务端默认的比对规则,排序规则
log-error=/var/log/mysql/error.log #(默认)错误日志地址
pid-file=/var/run/mysqld/mysqld.pid #(默认)进程文件所在目录
default-time_zone = '+8:00'
```
Tips:
`sudo mkdir -p /var/run/mysqld` `sudo chmod 757 /var/run/mysqld/` `chown -R mysql:mysql /var/run/mysqld`
`sudo mkdir -p /var/log/mysql`
`sudo chown -R mysql:mysql /var/lib/mysql`
- 环境变量
`echo "export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/sbin" >> /etc/profile`
`sudo source /etc/profile`
- 服务启动:
`sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/`
`sudo service mysql.server start`
`sudo /etc/init.d/mysql.server start`
- 开机启动:
`sudo chkconfig mysql.server on`
- 登录后修改密码:
`alter user user() identified by "123456";`
## 忘记密码操作
- 忘记登录密码: `vim /etc/mysql/my.cnf`
```shell
[mysqld]
skip-grant-tables #跳过用户的权限检查
```
- 重启mysql.server
```mysql
sell> mysql #登录
mysql> use mysql; #选择数据库
mysql> FLUSH PRIVILEGES; #重新加载grant table
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MyNewPass'; #修改密码
mysql> quit
```
- 恢复配置文件
```shell
[mysqld]
skip-grant-tables #跳过用户的权限检查
```