Mysqld

安装mysql

由于从centos7之后,官方yum仓库默认的mysql数据库变成了mariadb,所以要安装mysql需要先导入mysql官方yum源

从官方yum源选择与你系统版本相同的源下载。我这里是CentOS7

1
2
$ wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
$ rpm -ivh mysql80-community-release-el7-3.noarch.rpm

安装好mysql源之后,安装mysql

1
2
3
4
5
6
7
$ yum install -y mysql
$ rpm -qa | grep mysql
mysql-community-common-8.0.20-1.el7.x86_64
mysql-community-client-8.0.20-1.el7.x86_64
mysql-community-libs-8.0.20-1.el7.x86_64
mysql-community-server-8.0.20-1.el7.x86_64
mysql80-community-release-el7-3.noarch

移动mysql数据库

复制数据库到新地址

将mysql数据库从/var/lib/mysql 移动到你想要的路径下,比如/db/mysql

1
$ cp -a /var/lib/mysql /db

注意用户权限

1
$ chown mysql:mysql /db/mysql

修改my.cnf

修改/etc/my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 修改前
$ cat /etc/my.cnf
[mysqld]
...
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

# 修改后
$ cat /etc/my.cnf
[mysqld]
...
datadir=/db/mysql
socket=/db/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

max_connections=600
[client]
socket=/db/mysql/mysql.sock

修改/usr/bin/mysqld_pre_systemd

替换所有/var/lib/db

1
2
3
$ vim /usr/bin/mysqld_pre_systemd

:%s/\/var\/lib/\/db/g

关闭selinux

由于selinux可能会导致mysqld无法启动,参考https://blogs.oracle.com/jsmyth/selinux-and-mysql

1
2
$ setenforce 0
$ systemctl restart mysqld

修改密码

通常来说在/var/log/mysqld.log中可以找到temporal password,但是如果找不到,那么就需要在新目录/db/mysql下重新初始化mysql。

1
2
3
4
5
6
7
$ systemctl stop mysqld
# 删除/db/mysql下的文件
$ rm -rf /db/mysql/*
# 这个命令生成的root有临时password
$ mysqld --initialize
# 这个命令生成的root没有password
$ mysqld --initialize-insecure

重启服务,设置密码策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ systemctl start msyqld
$ mysql -u root -p
password:
mysql> show variables like "validate_password%";
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password.check_user_name    | ON    |
| validate_password.dictionary_file    |       |
| validate_password.length             | 5     |
| validate_password.mixed_case_count   | 1     |
| validate_password.number_count       | 1     |
| validate_password.policy             | LOW   |
| validate_password.special_char_count | 1     |
+--------------------------------------+-------+
7 rows in set (0.00 sec)

mysql> set global validate.password.length=5;
mysql> set global_validate.policy 0

设置好密码策略之后,我们就可以设置最简单的密码了

1
$ mysql_secure_installation

注意:要允许root用户远程登录

hammerdb测试

使用buildschema.tcl生成数据

1
2
$ ./hammerdbcli
hammerdb> source script/buildschema.tcl

使用tpcc.tcl测试

1
2
$ nohup ./hammerdbcli auto script/tpcc.tcl &
$ exit