文章目录
清除系统自带MySQL
root@test>rpm -qa | grep mysql
akonadi-mysql-1.9.2-4.el7.x86_64
qt-mysql-4.8.5-11.el7.x86_64
root@test>rpm -e akonadi-mysql-1.9.2-4.el7.x86_64
root@test>rpm -e qt-mysql-4.8.5-11.el7.x86_64
root@test>rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.x86_64
mariadb-server-5.5.44-2.el7.x86_64
mariadb-5.5.44-2.el7.x86_64
root@test>rpm -e mariadb-server-5.5.44-2.el7.x86_64
root@test>rpm -e mariadb-5.5.44-2.el7.x86_64
由于mariadb-libs有依赖关系,暂且不删除,删除原有配置文件/etc/my.cnf
root@test>rm -rf /etc/my.cnf
创建mysql用户和mysql组
如果已创建则忽略(-s /bin/false参数指定mysql用户仅拥有所有权,而没有登录权限)
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
下载tar包并解压
访问官网下载指定tar包,上传本地CentOS7服务器,将tar包解压,并重命名
root@test>tar -xvf mysql-5.7.25-el7-x86_64.tar
root@test>tar -zxvf mysql-5.7.25-el7-x86_64.tar.gz -C /usr/local
root@test>mv /usr/local/mysql-5.7.25-el7-x86_64 mysql
CentOS环境变量配置
修改/etc/profile,在文件最后添加MySQL bin路径,并保存
export PATH=$PATH:/usr/local/mysql/bin
使/etc/profile配置文件生效
source /etc/profile
修改MySQL启动脚本
- 备份脚本(修改原始脚本前,优先备份)
root@test:/usr/local/mysql>cp ./support-files/mysql.server ./support-files/mysql.server.bak.20191022
- 修改启动脚本,改为当前MySQL目录
仅列出修改的脚本
# Set some defaults
# 自定义pid的路径,可以使用默认路径
mysqld_pid_file_path=/iot/mysql/data/mysqld.pid
if test -z "$basedir"
then
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
if test -z "$datadir"
then
# 自定义data目录的路径,data目录最好与日志及binlog日志路径放在不同磁盘,以提高写入效率
datadir=/iot/mysql/data
fi
sbindir=/usr/local/mysql/bin
libexecdir=/usr/local/mysql/bin
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
创建data文件夹,并修改权限及用户组
root@test:/iot/mysql>mkdir data
root@test:/iot/mysql>chmod 770 -R data/
root@test:/iot/mysql>chown -R mysql:mysql data/
MySQL所在路径权限配置
root@test>chown -R mysql:mysql /usr/local/mysql/
如果MySQL basedir还有父级目录,父级目录的权限需要为755
配置文件
新建配置文件/etc/my.cnf
[mysqld]
port=3306
datadir=/iot/mysql/data
socket=/iot/mysql/mysql.sock
user=mysql
max_connections=151
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 区分大小写
lower_case_table_names = 0
# 指定编码
character-set-server=utf8
collation-server=utf8_general_ci
log_timestamps=SYSTEM
default-time-zone='+8:00'
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[mysqld_safe]
log_error=/iot/mysql/log/mysqld.log
pid_file=/iot/mysql/data/mysqld.pid
#指定客户端连接mysql时的socket通信文件路径
[client]
socket=/iot/mysql/mysql.sock
default-character-set=utf8
修改my.cnf权限
root@test:/iot>chmod 644 /etc/my.cnf
注意这里权限不能大于644,否则会提示
[Warning] World-writable config file '/etc/my.cnf' is ignored.
大概意思是权限全局可写,任何一个用户都可以写。mysql担心这种文件被其他用户恶意修改,所以忽略掉这个配置文件。
根据log_error=/iot/mysql/log/mysqld.log
路径新建log文件
root@test:/iot/mysql>cd log/
root@test:/iot/mysql/log>touch mysqld.log
root@test:/iot/mysql/log>ll
总用量 0
-rw------- 1 root root 0 2019-03-05 16:07:35 mysqld.log
root@test:/iot/mysql/log>chown mysql:mysql mysqld.log
# 权限改为666更靠谱
root@test:/iot/mysql/log>chmod 666 mysqld.log
初始化MySQL
root@test:/usr/local/mysql>./bin/mysqld --user=mysql --basedir=/usr/local/mysql --log-error=/iot/mysql/log/mysqld.log --datadir=/iot/mysql/data --initialize
root@test:/usr/local/mysql> tailf /iot/mysql/mysql-5.7.25/log/mysqld.log
2019-03-05T06:20:37.283941Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-03-05T06:20:37.903864Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-03-05T06:20:38.006903Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-03-05T06:20:38.071675Z 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: cb146e07-3f0e-11e9-a5f0-0050568d27e1.
2019-03-05T06:20:38.075188Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-03-05T06:20:38.076639Z 1 [Note] A temporary password is generated for root@test: s6Yhwst?A.Ax
记住root账户登陆密码:++s6Yhwst?A.Ax++
配置系统命令
复制启动脚本到资源目录
root@test:/> cd /usr/local/mysql
root@test:/usr/local/mysql> cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
增加mysqld服务控制脚本执行权限
root@test:/usr/local/mysql> chmod +x /etc/rc.d/init.d/mysqld
将mysqld服务加入到系统服务
root@test:/usr/local/mysql> chkconfig --add mysqld
检查mysqld服务是否已经生效
root@test:/usr/local/mysql> chkconfig --list mysqld
mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
启动MySQL
root@test:/usr/local/mysql> systemctl start mysqld
修改root账户密码
- 利用上述密码
++s6Yhwst?A.Ax++
登陆MySQL,修改默认密码
alter user 'root'@'localhost' identified by '123456';
- 设置允许远程连接数据库
use mysql;
update user set user.Host='%' where user.User='root';
select user,host from user;
flush privileges;
设置完成之后,就可以远程使用user=root, password=123456
登陆MySQL了。