MySQL 是目前最流行的关系型开源数据库之一,由于其开源,很受中小企业的青睐;因此掌握其使用也是作为一枚技术的必备打怪技能之一;
系统环境:CentOS Linux release 7.6.1810 (Core)
MySQL版本:MySQL 5.7.28(mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz)
官方下载:https://dev.mysql.com/downloads/mysql/
mkdir -p /data/mysql # 数据仓库目录
mkdir -p /opt/mysql # 安装目录
groupadd mysql # 添加 mysql 用户组
useradd -r -g mysql mysql -s /bin/false # 添加 msyql用户禁止登录shell
chown -R mysql:mysql /data/mysql
chown -R mysql:mysql /opt/mysql
yum install -y numactl-libs libaio # 安装依赖
tar -xvf mysql-5.7.28-linux-glibc2.12-x86_64.tar -C /opt # 解压文件
ln -s mysql-5.7.28 mysql # 生成软连接,便于版本管理
cd /opt/mysql/bin # 切换到mysqld 目录
./mysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql --datadir=/data/mysql --user=mysql --initialize
初始化:
#############
[root@docker bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
2020-07-01T05:21:18.378589Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-07-01T05:21:19.303643Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-07-01T05:21:19.433393Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-07-01T05:21:19.518458Z 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: b1f34d3c-bb5a-11ea-9911-000c2948dd1c.
2020-07-01T05:21:19.520205Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-07-01T05:21:20.528572Z 0 [Warning] CA certificate ca.pem is self signed.
2020-07-01T05:21:20.598496Z 1 [Note] A temporary password is generated for root@localhost: `!sa2yywEydgb` ## 临时root 密码注意保存
###############
生成SSL 文件:
[root@docker mysql]# bin/mysql_ssl_rsa_setup --datadir=/opt/mysql
Generating a 2048 bit RSA private key
...............................................................+++
.......................................................+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
.................................................+++
............................................................................................................................................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
.......+++
..........................................+++
writing new private key to 'client-key.pem'
-----
[root@docker Tools]# vim /etc/my.cnf
[mysqld]
basedir=/opt/mysql # 安装目录
datadir=/data/mysql # 数仓目录
port=3306 # 服务端口
user=mysql # 启动用户
bind-address=0.0.0.0 # 绑定网卡
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
err-log==/var/log/mysql/mysqld.log # 错误日志
pid-file=/opt/mysql/mysqld.pid # pid 文件
[root@docker Tools]# vim /etc/profile
# MySQL 家目录
MYSQL_ROOT=/opt/mysql
PATH=$PATH:$HOME/bin:$MYSQL_ROOT/bin
export PATH
默认basedir
和datadir
是
mysqld_pid_file_path=
if test -z "$basedir"
then
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
if test -z "$datadir"
then
datadir=/usr/local/mysql/data
fi
sbindir=/usr/local/mysql/bin
libexecdir=/usr/local/mysql/bin
修改为自定义目录
[root@docker support-files]# vim /opt/mysql/support-files/mysql.server
复制修改后的server 服务到 /etc/init.d/mysql
[root@docker support-files]# cp mysql.server /etc/init.d/mysql
通过server 管理mysql
[root@docker support-files]# service mysql status # 查看当前MySQL服务状态
SUCCESS! MySQL running (42113)
[root@docker support-files]# service mysql restart #重启MySQL服务
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@docker support-files]# service mysql stop # 停止MySQL 服务
Shutting down MySQL.. SUCCESS!
# chmod 755 /etc/init.d/mysql # 修改权限
# chkconfig --add mysql # 添加自启动
# chkconfig --level 345 mysql on # 添加不同级别的配置
更新临时密码:
[root@docker ~]# mysql -uroot -p # 使用临时密码登录MySQL
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28 MySQL Community Server (GPL)
mysql> set password=password('passwod'); # 更新默认密码
mysql>grant all privileges on *.* to 'root'@'%' identified by 'password'; #授权 root 用户可以在任何主机远程登录
mysql>flush privileges; # 刷新权限
查看当前的用户登录主机
mysql> select host,user from mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| % | test1 |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
4 rows in set (0.00 sec)
firewall-cmd --permanent --add-service=mysql
firewall-cmd --reload
firewall-cmd --permanent --list-all
public # public 域
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client mysql # 启用的服务
查看MySQL监听状态:
通过端口:
[root@docker ~]# ss -ntpl | grep mysql
LISTEN 0 80 *:3306 *:* users:(("mysqld",pid=95559,fd=31))
通过远程Telnet
telnet IP 3306
#########################################################################
[root@docker opt]# /opt/mysql/bin/mysqld_safe
2020-07-01T05:31:58.293272Z mysqld_safe Logging to ‘/data/mysql/docker.com.err’.
2020-07-01T05:31:58.319322Z mysqld_safe Starting mysqld daemon with databases from /data/mysql
2020-07-01T05:31:58.656425Z mysqld_safe mysqld from pid file /opt/mysql/mysqld.pid ended
【ERROR】
2020-07-01T05:31:05.175679Z 0 [ERROR] /opt/mysql/bin/mysqld: Can’t create/write to file ‘/opt/mysql/mysqld.pid’ (Errco
de: 13 - Permission denied)
2020-07-01T05:31:05.175689Z 0 [ERROR] Can’t start server: can’t create PID file: Permission denied
###########################################################################
【解决】:
检查 hostname.err文件,默认位置在$datadir中,tail -n 100看一下是否有ERROR,通过上面的Error 看出是由于PID文件无权限创建,因此,增加对PID存放目录的权限
chown -R mysql:mysql /opt/mysql
配置文件my.cnf
配置sock 存放目录,导致无法正常连接MySQL ,
##############################################################
[root@docker ~]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
############################################################
【解决】
不进行sock存放配置;暂时处理