centos7 安装mysql5.7

  1. 创建用户和组
1
2
   groupadd mysql 
   useradd -g mysql -s /sbin/nologin mysql
  1. 解压到指定目录并重命名
1
2
	#dev.mysql.com 下载 linux 通用二进制包
	tar xzvf XXX.tar.gz /usr/local/mysql
  1. 配置环境变量
1
2
3
   echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile

   source /etc/profile
  1. 创建数据目录,并配置权限
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
   mkdir -p /data/mysql/{data,binlogs,log,etc,run}

   ln -s /data/mysql/data  /usr/local/mysql/data

   ln -s /data/mysql/binlogs  /usr/local/mysql/binlogs

   ln -s /data/mysql/log  /usr/local/mysql/log

   ln -s /data/mysql/etc  /usr/local/mysql/etc

   ln -s /data/mysql/run  /usr/local/mysql/run

   chown -R mysql.mysql   /data/mysql/  # 设置文件夹为第一步创建的用户组、用户的权限

   chown -R mysql.mysql   /usr/local/mysql/{data,binlogs,log,etc,run} # 设置文件夹为第一步创建的用户组、用户
  1. 删除系统自带的my.cnf
1
   rm -f /etc/my.cnf
  1. 在/usr/local/mysql/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
24
25
26
27
28
29
30
31
32
   [client]
   port = 3306
   socket = /usr/local/mysql/run/mysql.sock

   

   [mysqld]
   pid_file = /usr/local/mysql/run/mysql.pid
   datadir = /usr/local/mysql/data
   socket = /usr/local/mysql/run/mysql.sock
   max_connections = 2048
   open_files_limit = 65535
   skip-name-resolve
   lower_case_table_names=1
   character-set-server = utf8mb4
   collation-server = utf8mb4_unicode_ci
   init_connect='SET NAMES utf8mb4'
   innodb_buffer_pool_size = 1024M
   innodb_log_file_size = 2048M
   innodb_file_per_table = 1
   innodb_flush_log_at_trx_commit = 0
   key_buffer_size = 64M
   log-error = /usr/local/mysql/log/mysql_error.log
   log-bin = /usr/local/mysql/binlogs/mysql-bin
   slow_query_log = 1
   slow_query_log_file = /usr/local/mysql/log/mysql_slow_query.log
   long_query_time = 5   
   tmp_table_size = 32M
   max_heap_table_size = 32M
   query_cache_type = 0
   query_cache_size = 0
   server-id=1
  1. 初始化mysql
1
   mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
  1. 记住生成的root密码
1
   grep 'temporary password' /usr/local/mysql/log/mysql_error.log
  1. 生成ssl
1
   mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
  1. 配置系统服务
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    cd /usr/lib/systemd/system 
    touch mysqld.service #并加入以下内容

    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    
    [Install]
    WantedBy=multi-user.target

    [Service]
    User=mysql
    Group=mysql
    Type=forking
    PIDFile=/usr/local/mysql/run/mysqld.pid    
    \# Disable service start and stop timeout logic of systemd for mysqld service.
    TimeoutSec=0
    \# Execute pre and post scripts as root
    PermissionsStartOnly=true
    \# Needed to create system tables
    \#ExecStartPre=/usr/bin/mysqld_pre_systemd
    \# Start main service
    ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/run/mysqld.pid $MYSQLD_OPTS
    \# Use this to switch malloc implementation
    EnvironmentFile=-/etc/sysconfig/mysql
    \# Sets open_files_limit
    LimitNOFILE = 65535    
    Restart=on-failure    
    RestartPreventExitStatus=1    
    PrivateTmp=false
  1. 启动mysql服务以及开机自启动
1
2
    systemctl enable mysqld
    systemctl start mysqld
  1. 用步骤8中的root密码进行登录,第一次登陆需要设置密码
1
alter user 'root'@'localhost' identified by 'root' # 只能通过该方法修改;
  1. 过程中遇到错误
1
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/run/mysql.sock' (2)

分析:在/usr/local/mysql/run/mysql.sock 位置没有找到mysql.sock;通过命令 find / -name mysqld.sock , 发现mysql。sock 位置不对,不是my.cnf配置位置,shy?

一番尝试发现 my.cnf,可以发现问题源头,不光[client] 中配置socket地址,[mysqld]中也要配置socket地址(上文已经更正错误),还是没有理解配置。。。

Built with Hugo     主题 StackJimmy 设计