# 安装,基本配置
# 一、安装
# 1.1 Ubuntu
- 安装
# Ubuntu系统
sudo apt update && sudo apt install -y mariadb-server
1
2
2
- 初始设置
mysql_secure_installation
1
- 开启远程访问
注释掉bind-address = 127.0.0.1
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
sudo service mysql restart
1
2
2
# 1.2 CentOS
- 从官方 (opens new window)下载对应版本RPM源包并安装
[root@master01 opt]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
1
- 删除原有MySQL
[root@master01 opt]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@master01 opt]# rpm -e --nodeps mariadb-libs
1
2
3
2
3
- 安装MySQL
[root@master01 ~]# yum install mysql-community-server
1
- 启动
[root@master01 ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
1
2
2
- root默认密码
# 安装完以后会自动创建账号`'root'@'localhost'`
grep 'temporary password' /var/log/mysqld.log
s,r)SrtPp3tU
1
2
3
2
3
- 修改密码
$> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 's,r)SrtPp3tU';
1
2
2
- 设置root用户允许任意ip连接
mysql> update mysql.user set host='%' where user='root';
mysql> flush privileges;
1
2
2
# 1.3 KubeSphere中部署
- 创建配置(ConfigMap)
配置
-->配置字典
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
init_connect='SET collation_connection = utf8mb4_unicode_ci'
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
- 创建pvc
存储
-->存储卷
- 部署mysql
应用负载
-->工作负载
-->有状态副本集
- 创建外部访问服务
应用负载
-->服务
# 1.4 docker
# 镜像拉取
docker pull mysql
# 容器运行
docker run --name msql -v /msql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=root -d mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
# 连接测试
docker exec -it 容器名 bash
mysql -uroot -p密码连接即可
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 二、基本配置
# 将默认字符集修改为utf8mb4
编辑/etc/mysql/conf.d/mysql.cnf
添加如下:
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
init_connect='SET collation_connection = utf8mb4_unicode_ci'
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 免密登陆
如果想要免密登录,那么就在HOME下创建一个 .my.cnf
,然后配置密码:
[client]
user = <用户名>
password = <密码>
1
2
3
2
3
设置好文件权限 chmod 400 ~/.my.cnf
# 设置定时备份
# 每天5点,备份mysql
0 5 * * * /usr/bin/mysqldump --single-transaction --quick --lock-tables=true --all-databases | gzip -c > /data/backup/mysql/full-backup-$(date +\%F).sql.gz
1
2
2
# 设置slowlog
slowlog是MySQL里定位慢查询的一个有力工具,为了开启slowlog,要做如下配置,编辑 /etc/mysql/mariadb.conf.d/50-server.cnf
, 在 [mariadb] 下加入:
slow_query_log
long_query_time=1.0
1
2
2
注意要touch这个文件并且确保权限正确:sudo touch /var/log/mysql-slow.log && sudo chown mysql:mysql /var/log/mysql-slow.log
还有一种方式就是设置一个全局变量:
mysql> SET GLOBAL slow_query_log=1;
mysql> SET GLOBAL long_query_time=1.0;
1
2
2
上述的 long_query_time
都是指,超过这个执行时间(单位是秒)的就记录到日志。如果再有慢日志,就可以在文件里看到SQL了。
账号权限 →