1、下载mysql5.7.21通用二进制版
[root@CentOS local]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
2、卸载系统自带的MariaDB
[root@CentOS ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64
[root@CentOS ~]# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64
3、创建mysql用户组和mysql用户
[root@CentOS local]# groupadd mysql
[root@CentOS local]# useradd -g mysql -r -s /bin/false mysql
//-r参数表示mysql用户是系统用户,-s 指定用户登入后所使用的shell。默认值为/bin/bash。设置为/bin/false不可用于登录系统
4、解压安装包,并将解压好的文件夹重命名为mysql
[root@CentOS local]# tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
mysql-5.7.21-linux-glibc2.12-x86_64/bin/myisam_ftdump
mysql-5.7.21-linux-glibc2.12-x86_64/bin/myisamchk
mysql-5.7.21-linux-glibc2.12-x86_64/bin/myisamlog
mysql-5.7.21-linux-glibc2.12-x86_64/bin/myisampack
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql_client_test_embedded
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql_config_editor
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql_embedded
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql_install_db
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql_plugin
......(略去内容).....
[root@CentOS local]# mv mysql-5.7.21-linux-glibc2.12-x86_64 mysql
5、support-files目录未发现my-default.cnf,需要创建:
vim /etc/my.cnf
#复制以下内容
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#不区分大小写
lower_case_table_names = 1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_connections=5000
default-time_zone = '+8:00'
6、将mysql添加为系统服务
[root@CentOS local]# cp mysql/support-files/mysql.server /etc/init.d/mysql
[root@CentOS local]# chkconfig --add mysql
将mysql服务设置为开机启动
[root@CentOS mysql]# chkconfig mysql on
7、初始化数据库
[root@CentOS mysql]# mkdir data
[root@CentOS mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2018-02-24T11:03:47.265873Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-02-24T11:03:48.343129Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-02-24T11:03:48.473269Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-02-24T11:03:48.541782Z 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: 63b1ed0c-1952-11e8-aa97-000c29847b2d.
2018-02-24T11:03:48.542867Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-02-24T11:03:48.543383Z 1 [Note] A temporary password is generated for root@localhost: =u>yyVorV7g%
[root@CentOS mysql]#
如上所示,在/usr/local/mysql目录下创建用于保存数据的data 目录,初始化数据库后获得的临时密码为:=u>yyVorV7g%可以在mysql_error.log文件找到
生成ssl
[root@CentOS mysql]# mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
8、启动mysql服务,使用临时密码登录mysql
[root@CentOS mysql]# systemctl start mysql # 使用 service mysql start 也可以
[root@CentOS mysql]# ps -ef | grep mysql
root 4654 1 0 19:07 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/CentOS.pid
mysql 4739 4654 4 19:07 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=CentOS.err --pid-file=/usr/local/mysql/data/CentOS.pid
root 4772 3618 0 19:07 pts/0 00:00:00 grep --color=auto mysql
[root@CentOS mysql]# ./bin/mysql -u root -p
Enter password: # 这里输入刚获取的临时密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
9、修改登录密码
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root'); # 将 root 登录密码修改为123456
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
10、设置任何远程主机都可以访问数据库
[root@CentOS bin]# ./mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.21 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all privileges on *.* to 'root' @'%' identified by 'root'; # 设置任何远程主机都可以访问
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges; # 刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql>
11、开放 3306 端口
[root@CentOS bin]# firewall-cmd --zone=public --add-port=3306/tcp --permanent # 添加 3306 端口
success
[root@CentOS bin]# firewall-cmd --reload # 重新载入
success
[root@CentOS bin]#
总结:
1.mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
find / -name mysql.sock
如果能查到结果的话,只需将查到的结果做一个软连接到/tmp目录下即可解决
ln -s /var/lib/mysql.sock /tmp/mysql.sock
连接localhost通常通过一个Unix域套接字文件进行,一般是/tmp/mysql.sock。如果套接字文件被删除了,本地客户就不能连接。这可能发生在你的系统运行一个cron任务删除了/tmp下的临时文件。
如果你因为丢失套接字文件而不能连接,你可以简单地通过重启服务器重新创建得到它。因为服务器在启动时重新创建它。
如果和我一样,重启服务器还是没有任何变化,你可以先执行下面的语句:
# mysql -uroot -h 127.0.0.1 -p
不出意外,这句话应该是可以执行的,你现在不能用套接字建立连接因为它不见了,所以可以建立一个TCP/IP连接
2.mysql: mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists
5.,7下,mysqld_safe启动实例报错
[root@test2 ~]# 2018-09-08T01:42:11.940420Z mysqld_safe Logging to '/var/log/mysqld/my57_3307.log'.
2018-09-08T01:42:11.949380Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
[1]+ Exit 1 /usr/local/mysql57/bin/mysqld_safe --defaults-file=/dbdata/mysql/my57_3307/my57_3307.cnf --ledir=/usr/local/mysql57/bin
解决办法:
[root@test2 ~]# vim /usr/local/mysql57/bin/mysqld_safe
# Check that directory for $safe_mysql_unix_port exists
mysql_unix_port_dir=`dirname $safe_mysql_unix_port`
if [ ! -d $mysql_unix_port_dir ]
then
mkdir $mysql_unix_port_dir
chown $user $mysql_unix_port_dir
chmod 755 $mysql_unix_port_dir
# Changed 5.7 setttings:
# log_error "Directory '$mysql_unix_port_dir' for UNIX socket file don't exists."
# exit 1
fi
5.7的mysqld_safe启动检查目录时,没有直接报错退出.改成5.6的建目录即可