数据库mysql-5.7.17的安装详解
1、安装前的准备工作
1.1、系统基本信息
为给安装过程减少麻烦,我这里已经提前关闭了防火墙和selinux。我这里是纯净的系统,没有安装过MySQL和mariadb,如果机器上有安装过MySQL和mariadb的请先卸载后再进行安装。
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@localhost ~]# ifconfig
eno16777736: flags=4163
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe48:bb2f prefixlen 64 scopeid 0x20
ether 00:0c:29:48:bb:2f txqueuelen 1000 (Ethernet)
RX packets 464050 bytes 681487546 (649.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 27659 bytes 3103086 (2.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
1.2、系统内存检查
安装5.6以及以上版本的mysql需要服务器的内存至少在1G以上。检查一下 linux 系统的内存大小,如果内存不足 1G,启动 mysql 的时候可能会产生下面这个错误提示:
Starting mysqld (via systemctl): Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.[FAILED]
1.3、下载并上传MySQL安装包到服务器
MySQL安装包下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
我这里下载和使用的安装包是 mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
1.4、规划好相关路径
安装包上传目录:/root
Mysql目录安装位置:/usr/local/mysql-5.7
数据库保存位置:/data/mysql-5.7
2、安装MySQL-5.7.17
2.1、解压缩并复制mysql-5.7.17到/usr/local下
[root@localhost ~]# tar -xf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
[root@localhost ~]# cp -a mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql-5.7
2.2、创建相关文件目录
[root@localhost ~]# mkdir -p /data/mysql-5.7
2.3、新建mysql用户和组(组会在创建用户时自动创建)
[root@localhost ~]# useradd -r -s /sbin/nologin mysql
[root@localhost ~]# more /etc/group|grep mysql
mysql:x:994:
2.4、检查是否安装了 libaio包
[root@localhost ~]# rpm -qa | grep libaio
libaio-0.3.109-13.el7.x86_64
2.5、给MySQL相关目录授权
[root@localhost mysql-5.7]# chown -R mysql:mysql /usr/local/mysql-5.7/
[root@localhost mysql-5.7]# chown -R mysql /data
[root@localhost mysql-5.7]# ls -l /data/
drwxr-xr-x 3 mysql root 22 9月 28 00:33 log
drwxr-xr-x 2 mysql root 6 9月 28 00:32 mysql-5.7
2.6、安装MySQL
[root@localhost ~]# cd /usr/local/mysql-5.7/
[root@localhost mysql-5.7]# ls
bin COPYING docs include lib man README share support-files
[root@localhost mysql-5.7]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-5.7/ --datadir=/data/mysql-5.7/
2018-09-28×××1:24:32.486082Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-28×××1:24:32.726180Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-28×××1:24:32.774352Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-28×××1:24:32.842600Z 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: 1295295f-c311-11e8-bd4b-000c2948bb2f.
2018-09-28×××1:24:32.846975Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-28×××1:24:32.848046Z 1 [Note] A temporary password is generated for root@localhost: .L9of#e46sSu #这个密码连接数据库需要用
3、MySQL的相关配置
3.1、配置my.cnf文件(这里只是简单的配置,生产环境需要更详细配置)
[root@localhost mysql-5.7]# cp -a support-files/my-default.cnf /etc/my.cnf
[root@localhost mysql-5.7]# vim /etc/my.cnf
[mysqld]
user = mysql
basedir = /usr/local/mysql-5.7
datadir = /data/mysql-5.7
# port = .....
# server_id = .....
character-set-server = utf8
socket = /data/mysql-5.7/sock/mysql.sock
pid-file = /data/mysql-5.7/sock/mysql.pid
log-error = /data/mysql-5.7/log/error.log
3.2、创建日志和pid目录并授权
在MySQL初始化之前,MySQL数据目录不能有任何文件和目录,不然会报错,所以sock和log目录需要在初始化完之后的这里创建。
[root@localhost mysql-5.7]# mkdir /data/mysql-5.7/sock
[root@localhost mysql-5.7]# mkdir /data/mysql-5.7/log
[root@localhost mysql-5.7]# chown -R mysql.mysql /data
3.3、创建错误日志文件并授权
上面配置文件中指定了错误日志,这里必须创建日志文件,不然启动MySQL时会报以下错误。
[root@localhost mysql-5.7]# ./support-files/mysql.server start
Starting MySQL.2018-09-28×××2:44:56.562761Z mysqld_safe error: log-error set to '/data/mysql-5.7/log/error.log', however file don't exists. Create writable for user 'mysql'.
ERROR! The server quit without updating PID file (/data/mysql-5.7/sock/mysql.pid).
解决方法:
[root@localhost mysql-5.7]# echo ' ' >/data/mysql-5.7/log/error.log
[root@localhost mysql-5.7]# chown mysql.mysql /data/mysql-5.7/log/error.log
3.4、启动MySQL
[root@localhost mysql-5.7]# ./support-files/mysql.server start
Starting MySQL.2018-09-28×××1:30:28.616264Z mysqld_safe The file /usr/local/mysql/bin/mysqld
does not exist or is not executable. Please cd to the mysql installation
directory and restart this script from there as follows:
./bin/mysqld_safe&
See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information
ERROR! The server quit without updating PID file (/data/mysql-5.7/sock/mysql.pid).
解决方法
[root@localhost mysql-5.7]# ln -s /usr/local/mysql-5.7 /usr/local/mysql
[root@localhost mysql-5.7]# ./support-files/mysql.server start
Starting MySQL. SUCCESS!
启动MySQL也可以使用下面的命令
[root@localhost mysql-5.7]# ./bin/mysqld_safe &
3.5、设置mysql使用service管理
[root@localhost mysql-5.7]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql-5.7]# chmod +x /etc/rc.d/init.d/mysqld
[root@localhost mysql-5.7]# chkconfig --add mysqld
[root@localhost mysql-5.7]# chkconfig --list mysqld
[root@localhost mysql-5.7]# echo 'export PATH=$PATH:/usr/local/mysql-5.7/bin/' >>/etc/profile
[root@localhost mysql-5.7]# source /etc/profile
[root@localhost mysql-5.7]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql-5.7/
datadir=/data/mysql
这里可以使用service命令启动和停止MySQL,无法使用systemctl启动和停止
[root@localhost mysql-5.7]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@localhost mysql-5.7]# service mysqld start
Starting MySQL. SUCCESS!
[root@localhost system]# mysql -u root –p #连接mysql报错
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
解决方法:
[root@localhost system]# ln -s /data/mysql-5.7/sock/mysql.sock /tmp/mysql.sock
[root@localhost system]# mysql -u root -p
Enter password: #这里输入的密码就是上面初始化时生成的密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17
………………………………………………
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases; #查看数据库报错,要求修改密码
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
#更改密码方法1
mysql> set password for root@localhost=password('123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
#更改密码方法2
mysql> update mysql.user set authentication_string=password('123456') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.6、设置mysql使用systemctl管理
[root@localhost ~]# cp -a /usr/local/mysql-5.7/support-files/mysql.server /usr/lib/systemd/system
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# vim 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
ExecStart=/usr/local/mysql-5.7/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000
说明:ExecStart=/usr/local/mysql-5.7/bin/mysqld是mysql安装目录中查找的二进制文件。
[root@localhost system]# systemctl stop mysqld
[root@localhost system]# systemctl start mysqld
[root@localhost system]# ss -ant|grep 3306
LISTEN 0 80 :::3306 :::*
到这里我们的MySQL就安装完成了。这里只是一个安装思路及过程,如果在生产环境使用,可以根据需要修改mysql的安装路径,数据存放路径,日志存放路径及日志格式,还需要设置防火墙开放3306端口,设置复杂的数据库密码,还有各种优化等需要做。