CentOS6.5安装mysql5.7

本文提供MySQL5.7在Linux环境下从下载安装包到完成配置的详细步骤,包括创建用户组、设置权限、初始化数据库、配置my.cnf、设置开机自启动等,并给出常见错误的排查方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.下载安装包

可以用.tar.gz或者.rpm包,这里我用的是mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz

2.建立用户以及用户组

groupadd mysql
useradd -r -g mysql mysql

3.建立安装包目录和安装目录

mkdir /usr/myfiles
mkdir /usr/works

4.通过Xftp将安装包放进/usr/myfiles文件夹下


5.安装到指定目录

tar -zxvf /usr/myfiles/mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz -C /usr/works



注意如果mysql-5.7.16-linux-glibc2.5-x86_64目录下没有data目录,手动建一个

6.目录权限设置

cd /usr/works/mysql-5.7.16-linux-glibc2.5-x86_64
chown mysql:mysql -R .
注意这里-R后面还有个 ” 空格  + . “  漏了的话会提示语法错误。  
7.初始化

/usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysqld --initialize --user=mysql --datadir=/usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/data --basedir=/usr/works/mysql-5.7.16-linux-glibc2.5-x86_64

成功后会有以下提示信息

201x-xx-xxT07:10:13.583130Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
201x-xx-xx T07:10:13.976219Z 0 [Warning] InnoDB: New log files created, LSN=45790
201x-xx-xx T07:10:14.085666Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
201x-xx-xx T07:10:14.161899Z 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: 1fa941f9-effd-11e5-b67d-000c2958cdc8.
201x-xx-xx T07:10:14.165534Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
201x-xx-xx T07:10:14.168555Z 1 [Note] A temporary password is generated for root@localhost: q1SLew5T_6K,

这里要注意,最后一行后面有一个初始密码,找个地方把它存起来,后面会用到!


8. my.cnf配置

将/usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/support-files/my-default.cnf 改名为my.cnf,再拷到/etc/下

cp /usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/support-files/my-default.cnf /etc/my.cnf

my.cnf配置

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

#log-bin=mysql-bin
#server-id=80

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

#安装路径
basedir = /usr/works/mysql-5.7.16-linux-glibc2.5-x86_64
#数据存放路径
datadir = /usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/data
#端口号
port = 3306
socket = /usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/temp/mysql.sock
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#default-character-set=utf8
character-set-server=utf8
init_connect='SET NAMES utf8'


wait_timeout=300
back_log = 500
max_connections = 3000
max_connect_errors = 6000
external-locking = FALSE
max_allowed_packet = 64M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 300
query_cache_size = 64M
query_cache_limit = 4M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 256M
max_heap_table_size = 256M
long_query_time = 2
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 512M
expire_logs_days = 7
key_buffer_size = 2048M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
lower_case_table_names = 1
innodb_buffer_pool_size = 2048M
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0


[client]
socket = /usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/temp/mysql.sock
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld_safe]
#日志路径
log-error=/var/log/mysql/mysqld.log

注意:/usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/下temp文件夹如果没有需要自己建,/var/log/下mysql也一样。


9.设置mysql运行权限及开机自启动

/usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/ support-files/mysql.server 拷

贝为/etc/init.d/mysql并设置运行权限

cp mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chown -R mysql:mysql /usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/

设置开机自启动

chkconfig --add mysql

10.客户端连接测试

开启服务

/usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysqld_safe&



/usr/works/mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysql -uroot -p

这时用户名root,密码就是前面让你保存下来的那个密码

如果出现以下错误,

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

请往上看,是否每一步做到位,改建的文件夹建了吗,有些文夹该给的权限给了

吗,去my.cnf里面设置的log文件目录里找log文件看具体是哪里出错了,一般都能解决。


连上后,记得修改密码

mysql> alter user 'root'@'localhost' identified by 'xxxxxxx';


最后,开启远程连接

mysql>grant all privileges on *.* to 'root'@'%' identified by 'rootpasswd' with grant option;

开放3306端口

/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT #开启3306端口  


此时,大功告成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值