安装mysql5.7.16, 操作系统为Centos7

时间:2022-02-27 00:48:07

1、下载mysql5.7.16安装包

#wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz


2.创建用户、创建安装文件夹

#groupadd mysql

#useradd -r -g mysql mysql


#mkdir /usr/local/mysql/

#chown -R mysql:mysql  /usr/local/mysql


3.解压,拷贝

#tar -xvzf mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz

#cd mysql-5.7.16-linux-glibc2.5-x86_64/

#mv * /usr/local/mysql/


--已安装,可省略

# yum install libaio


4、创建日志目录

# mkdir /var/log/mariadb/

# chown mysql:mysql /var/log/mariadb/


# mkdir /var/run/mariadb

# chown mysql:mysql /var/run/mariadb


如果不创建分别会出现如下错误:

2016-12-07T02:56:33.741622Z mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.

touch: 无法创建"/var/log/mariadb/mariadb.log": 没有那个文件或目录

chmod: 无法访问"/var/log/mariadb/mariadb.log": 没有那个文件或目录


[ERROR] /usr/local/mysql/bin/mysqld: Can't create/write to file '/var/run/mariadb/mariadb.pid' (Errcode: 2 - No such file or directory)


5、修改 my.cnf 配置文件

将 my.cnf 文件的 [mysqld] 下的socket,修改为 socket=/tmp/mysql.sock


# vi /etc/my.cnf

[mysqld]

socket=/tmp/mysql.sock


否则,连接的时候会出现以下问题:

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


6、初始化数据库

# cd /usr/local/mysql

# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql


出现以下语句表示成功,并显示root临时密码。

2016-12-07T02:56:02.996240Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2016-12-07T02:56:04.248210Z 0 [Warning] InnoDB: New log files created, LSN=45790

2016-12-07T02:56:04.448723Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2016-12-07T02:56:04.528814Z 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: b192ea07-bc28-11e6-bde9-005056893892.

2016-12-07T02:56:04.533654Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2016-12-07T02:56:04.535322Z 1 [Note] A temporary password is generated forroot@localhost: e_q:o+ief66M


7、启动数据库

# ./bin/mysqld_safe --user=mysql


查看日志是否成功:

# cat /var/log/mariadb/mariadb.log


2016-12-07T02:58:19.324018Z 0 [Note] /usr/local/mysql/bin/mysqld: ready for connections.

Version: '5.7.16'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server (GPL)


8、修改root用户密码

# ./bin/mysql -uroot -p

mysql> 

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';

mysql> flush privileges;


9、设置系统自动启动

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

# chkconfig --add mysqld
# chkconfig --level 2345 mysqld on


10、添加环境变量

vi /etc/profile

在最后添加

export MYSQL_HOME="/usr/local/mysql"
export PATH="$PATH:$MYSQL_HOME/bin"


刷新环境变量

# source  /etc/profile

至此可以使用 service mysqld start | stop 来启停mysql 服务了。


11、修改mysql,修改字符集编码为utf8,设置大小写不敏感

# vi /etc/my.cnf

[mysqld]

basedir=/usr/local/mysql

datadir=/var/lib/mysql

socket=/tmp/mysql.sock


lower_case_table_names=1   --此处为修改字符集

character_set_server=utf8 

init_connect='SET NAMES utf8'              --此处为英文单引号,博客显示问题。


12、重启数据库

# service mysqld stop

Shutting down MySQL.. SUCCESS! 

# service mysqld start

Starting MySQL. SUCCESS!


最后作个小广gao,个人开发的数据API平台,多种数据API,免费提供,地址 云数达 http://www.yunshuda.cn