在Linux机器上安装MySQL

在Linux机器上安装MySQL,仔细认真些就没有问题。

CentOS 7下MySQL 5.7安装、配置与应用_数据库技术_Linux公社-Linux系统门户网站

搞不定的话,直接删掉这个MySQL,然后把/etc/my.cnf也干掉。重新安装一边,Linux系统没有注册表之类的玩意,安装很简单。

比较容易出错的地方就是这个/etc/my.cnf:

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8 
socket=/usr/local/lib/mysql-5.7.18/tmp/mysql.sock
 
[mysqld]
skip-name-resolve
#设置3306端口
port = 3306 
user = mysql
socket=/usr/local/lib/mysql-5.7.18/tmp/mysql.sock # 设置mysql的安装目录 basedir=/usr/local/lib/mysql-5.7.18 # 设置mysql数据库的数据的存放目录 datadir=/usr/local/lib/mysql-5.7.18/data # 允许最大连接数 max_connections=200 # 服务端使用的字符集默认为8比特编码的latin1字符集 character-set-server=utf8 # 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB max_allowed_packet=16M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

这里的Socket很重要,配不对就会报这种错:

Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 这类的,

MySQL也分服务器和客户端,服务器端先开,在指定的位置建个Socket,客户端去连的时候,去找这个Socket。

如果没设置好,肯定是连不上的。

my.cnf里原来有的:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

                                                                                                                                                  Winter_Rollback

CentOS 7下MySQL 5.7安装、配置与应用_数据库技术_Linux公社-Linux系统门户网站

一、下载通用安装二进制包
 
先下载mysql安装包:打开 http://dev.mysql.com/downloads/mysql/
选择 linux - Generic并在其下选择
Linux - Generic (glibc 2.5) (x86, 64-bit), Compressed TAR Archive
进行下载。可以先下载到一个临时目录里,解压后,得到两个包:
mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz 
mysql-test-5.7.11-linux-glibc2.5-x86_64.tar.gz
只需要mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz 这个包就行了。
 
二、建立用户和目录
 
建立用户mysql,组mysql。后面mysql就使用这个用户来运行(注意这也是mysql启动脚本中默认的用户,因此最好不要改名)。
#groupadd mysql
#useradd -r -g mysql mysql
(使用-r参数表示mysql用户是一个系统用户,不能登录)
 
建立目录/work/program,后面mysql就安装在这个目录下面。
#mkdir /work/program
 
三、安装
 
【解压】
将前面得到的mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz解压至/work/program目录下
#tar zxvf mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz -C /work/program
 
这时在program下得到的目录名很长,如果不想改名,则可以建立一个联接:
#ln -s mysql-5.7.11-linux-glibc2.5-x86_64 mysql
此后就可以用/work/program/mysql来找到mysql的安装目录了
 
注意,如果mysql目录下没有data目录,手动建一个。
 
【目录权限设置】
将mysql及其下所有的目录所有者和组均设为mysql:
#cd /work/program/mysql
#chown mysql:mysql -R .
 
【初始化】
#/work/program/mysql/bin/mysqld --initialize --user=mysql --datadir=/work/program/mysql/data --basedir=/work/program/mysql
注意:
1. data目录解压后没有,需要手动建立(见上文);
2. mysql5.7和之前版本不同,很多资料上都是这个命令
...../scripts/mysql_install_db --user=mysql
 而5.7版本根本没有这个。
 
初始化成功后出现如下信息:
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,
 
注意最后一行,这也是和之有版本不同的地方,它给了root一个初始密码,后面要登录的时候要用到这个密码。
 
【配置】
将mysql/support-files下的my-default.cnf改名为my.cnf,拷到/etc下(或者考到{mysql}下,然后作一个软链接到/etc下):
#cp /work/program/mysql/support-files/my-default.cnf /etc/my.cnf
my.cnf中关键配置:
[mysqld]
basedir = /work/program/mysql
datadir = /work/program/mysql/data
port = 3306
socket = /work/program/mysql/tmp/mysql.sock
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
 
注意,tmp目录不存在,请创建之。
 
如果不把my.cnf拷到/etc下,运行时会出现:
mysqld: Can't change dir to '/usr/local/mysql/data/' (Errcode: 2 - No such file or directory)
这样的出错提示,说明它没找到my.cnf中的配置;而去找了程序编译时的默认安装位置:/usr/local/mysql
 
四、运行
 
【运行服务器程序】
#{mysql}/bin/mysqld_safe&
注:在这个启动脚本里已默认设置--user=mysql;在脚本末尾加&表示设置此进程为后台进程,区别就是在控制台输入bg,即可将当前进程转入后台,当前shell可进行其他操作。
【停止mysql】
{mysql}/bin/mysqladmin -uroot -p
(注意此时的root是指mysql的root用户)
 
五、设置mysql以服务运行并且开机启动
 
将{mysql}/ support-files/mysql.server 拷贝为/etc/init.d/mysql并设置运行权限
 
#cp mysql.server /etc/init.d/mysql
#chmod +x /etc/init.d/mysql
 
把mysql注册为开机启动的服务
#chkconfig --add mysql
 
当然也可以手动进行服务的开启和关闭:
#/etc/init.d/mysql start
#/etc/init.d/mysql stop
 
 
六、客户端连接测试
 
#{mysql}/bin/mysql -uroot -p
此时要求输入密码,就是前面初始化时生成的密码。
这时如果连接服务的时候出现错误:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
则需要在在my.cnf中填加:
[client]
socket = /work/program/mysql/tmp/mysql.sock
 
连上后,在做任何操作前,mysql要求要改掉root的密码后才能进行操作。
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by 'xxxxxxx';
 
七、TIPS
 
【查看mysql是否运行】
ps -ef|grep mysqld
netstat -lnp | grep -i mysql
 
【mysql启动时读取配置文件my.cnf的顺序】
可以运行如下命令查看:
./bin/mysqld --verbose --help |more
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
可以看到,启动时可以从上述目录下读取配置文件my.cnf。如果当前my.cnf文件不位于上述位置,则必须考过去或做链接。

MySQL 5.7新特性之Generated Column(函数索引) http://www.linuxidc.com/Linux/2016-02/128066.htm

升级到MySQL 5.7 解决分区问题  http://www.linuxidc.com/Linux/2016-02/128060.htm

MySQL 5.7 完美的分布式事务支持 http://www.linuxidc.com/Linux/2016-02/128053.htm

MySQL 5.7 新特性详解 http://www.linuxidc.com/Linux/2016-01/127636.htm

MySQL 5.7.11 发布下载 http://www.linuxidc.com/Linux/2016-02/128268.htm

在 CentOS 7 中以命令行方式安装 MySQL 5.7.11 for Linux Generic 二进制版本  http://www.linuxidc.com/Linux/2016-03/129187.htm

更多CentOS相关信息见CentOS 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=14

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-04/130414.htm

原文地址:https://www.cnblogs.com/duasonir/p/7152124.html