MySQL下载地址与Centos7安装MySQL以及启动问题排查
启动问题(如The server quit without updating PID file)
mysql等命令没有此命令或者不想每次都使用/usr/local/mysql/bin
执行mysql出现Can't connect to local MySQL server through socket '/tmp/mysql.sock'或者 ‘/var/lib/mysql/mysql.sock’等问题
》》请查看最后的附录进行解决《《
一、MySQL国内镜像下载
- 网易:http://mirrors.163.com/mysql/
- 中科大:http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/
- 清华:https://mirrors.tuna.tsinghua.edu.cn/mysql/
二、国内镜像相关站点
只列取部分(其他相关软件可进入站点进行下载)
三、Centos7安装MySQL5.7
1. 下载并解压至/usr/local
镜像站下载(以版本mysql-5.7.31-linux-glibc2.12-x86_64为例)
wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
解压至/usr/local
tar -xvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
重命名
cd /usr/local
mv mysql-5.7.31-linux-glibc2.12-x86_64/ mysql
2. 配置信息
新建my.cnf
mkdir -p /usr/local/mysql/etc
vi /usr/local/mysql/etc/my.cnf
在my.cnf中填入以下内容
[mysqld]
user=mysql
server-id=1
port = 3306
character_set_server=utf8mb4
datadir=/mnt/mysql/mysql_data
tmpdir=/mnt/mysql/mysql_tmp
# 日志
log-error=/mnt/mysql/log-error.txt
default-storage-engine=INNODB
join_buffer_size = 512M
tmp_table_size = 1G
max_allowed_packet = 64M
# 365
interactive_timeout = 31536000
# 24.86
wait_timeout = 2147483
read_buffer_size = 128M
read_rnd_buffer_size = 256M
sort_buffer_size = 256M
key_buffer_size=512M
back_log=500
flush_time=0
open_files_limit=4161
table_definition_cache=1400
binlog_row_event_max_size=16M
# 最大连接数
max_connections=1000
max_connect_errors = 1000
# 线程缓存大小
thread_cache_size=500
# 日志缓冲刷新的频繁程度
innodb_flush_log_at_trx_commit=2
innodb_buffer_pool_size=2G
innodb_log_file_size=512M
innodb_log_buffer_size=256M
innodb_thread_concurrency=32
innodb_autoextend_increment=64
innodb_buffer_pool_instances=8
innodb_concurrency_tickets=5000
innodb_old_blocks_time=1000
innodb_open_files=300
innodb_stats_on_metadata=0
innodb_file_per_table=1
innodb_checksum_algorithm=0
innodb_lock_wait_timeout = 50
# 瓶颈位
query_cache_size = 0
slow-query-log=1
long_query_time=10
lower_case_table_names=1
table_open_cache=4096
autocommit = 1
skip_name_resolve = 1
transaction_isolation = READ-COMMITTED
explicit_defaults_for_timestamp = 1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
注:
my.cnf配置文件的位置在:/usr/local/mysql/etc/
启动错误日志在:/mnt/mysql/log-error.txt
MySQL的wait_timeout连接已经修改为最大值,不会出现相关超时问题
3. 用户及用户组管理(提高安全)
新建组和用户
groupadd mysql
useradd -g mysql mysql -s /usr/sbin/nologin
创建数据目录(与my.cnf一致)
mkdir -p /mnt/mysql/mysql_data
mkdir -p /mnt/mysql/mysql_tmp
添加权限
chmod 644 /usr/local/mysql/etc/my.cnf
chmod 750 /mnt/mysql/mysql_data
chmod 750 /mnt/mysql/mysql_tmp
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /mnt/mysql/mysql_data
chown -R mysql:mysql /mnt/mysql/mysql_tmp
其他权限设置
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql /var/log/mariadb/mariadb.log
4. 初始化数据库
进入目录
cd /usr/local/mysql/
初始化数据
./bin/mysqld --initialize
若有错误发生
message:error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
solution:yum -y install numactl.x86_64
message: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
solution:yum -y install libaio
5. 启动数据库
#启动
/usr/local/mysql/support-files/mysql.server start
#停止
/usr/local/mysql/support-files/mysql.server stop
#重启
/usr/local/mysql/support-files/mysql.server restart
加入系统服务可以方便的启动停止MySQL服务(可选)
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
则MySQL启动方式为:
#开启
service mysqld start
#关闭
service mysqld stop
#重启
service mysqld restart
若启动有错误发生
如:The server quit without updating PID file (/mnt/mysql/mysql_data/xx.pid)!!首先查看启动错误日志信息:/mnt/mysql/log-error.txt
根据启动日志进行排查问题(直接百度一般都没啥用,切记根据日志进行定点查问题)
以及/var/log/mariadb/mariadb.log的日志
6. 默认密码修改
(1)先停止mysql服务
/usr/local/mysql/support-files/mysql.server stop
# 并查看进程,若有则kill
ps -ef|grep mysqld
结果如图所示
(2)启动 mysql 进入无需授权模式
./bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --skip-grant-tables --console
(3)新开一个窗口,并进入mysql目录下,执行以下命令连上数据库
./bin/mysql
(4)执行相关sql命令
-- 切换库
use mysql;
-- 更新密码为123456
update user set authentication_string=password("123456"),host = '%', password_expired='N' where user="root";
-- 查询结果
select user, authentication_string, host, password_expired from user;
FLUSH PRIVILEGES;
exit
7. 防火墙开放端口设置
开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
或者直接关闭防火墙(本地推荐)
# 启动
systemctl start firewalld
# 关闭
systemctl stop firewalld
# 查看状态
systemctl status firewalld
# 开机禁用
systemctl disable firewalld
# 开机启用
systemctl enable firewalld
附录:
1. 启动问题排查
如:The server quit without updating PID file (/mnt/mysql/mysql_data/xx.pid)
!!首先查看启动错误日志信息:/mnt/mysql/log-error.txt
- 添加日志方法:
# 修改my.cnf配置文件,增加以下内容
log-error=/mnt/mysql/log-error.txt
- 同时对以下文件增加权限
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql /var/log/mariadb/mariadb.log
然后根据启动日志进行排查问题(直接百度一般都没啥用,切记根据日志进行定点查问题)
或者/var/log/mariadb/mariadb.log的日志
若仍解决不了,可根据本文过程重新安装
重装MySQL过程数据库迁移注意点以及数据恢复,请看下篇内容MySQL数据库迁移与MySQL数据库批量恢复!
2. mysql等命令使用出现问题
(1)mysql等命令没有此命令或者不想每次都使用/usr/local/mysql/bin
加入环境变量
vim /etc/profile
文件末尾加入
# mysql
export PATH=/usr/local/mysql/bin:$PATH
source /etc/profile
(2)执行mysql出现Can't connect to local MySQL server through socket '/tmp/mysql.sock'等问题
这里若报Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’也是一样
首先查看mysql.sock的真正位置
然后可以对真正位置添加软连接到报错位置即可
启动mysql服务
查找mysql.sock的位置
[root@192 ~]# find / -name mysql.sock
/var/lib/mysql/mysql.sock
可以直接添加软连接
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock