CentOS7 安装Mysql5.7(解压缩版)
#### 1、下载安装包
http://dev.mysql.com/downloads/mysql/#downloads
推荐下载通用安装方法的TAR包
2.检查库文件是否存在,如有删除。
[root@localhost Desktop]$ rpm -qa | grep mysql
mysql-libs-.-.el6_..x86_64
[root@localhost ~]$ rpm -e mysql-libs-..x86_64 --nodeps
[root@localhost ~]$
3.检查mysql组和用户是否存在,如无创建。
[root@localhost ~]$ cat /etc/group | grep mysql
mysql:x::
[root@localhost ~]$ cat /etc/passwd | grep mysql
mysql:x::::/home/mysql:/bin/bash
以上为默认存在的情况,如无,执行添加命令:
[root@localhost ~]$groupadd mysql
[root@localhost ~]$useradd -r -g mysql mysql
//useradd -r参数表示mysql用户是系统用户,不可用于登录系统。
4.解压TAR包,更改所属的组和用户
[[email protected] ~]$ cd /usr/local/
[[email protected] local]$ tar xvf mysql--linux-glibc2-x86_64.tar
[[email protected] local]$ ls -l
total
-rwxr--r--. root root Jun : mysql--linux-glibc2-x86_64.tar
-rw-r--r--. wheel Mar : mysql--linux-glibc2-x86_64.tar.gz
-rw-r--r--. wheel Mar : mysql-test--linux-glibc2-x86_64.tar.gz
[[email protected] local]$ tar xvfz mysql--linux-glibc2-x86_64.tar.gz
[[email protected] local]$ mv mysql--linux-glibc2-x86_64 mysql
[[email protected] local]$ ls -l
total
drwxr-xr-x. root root Dec bin
drwxr-xr-x. root root Dec etc
drwxr-xr-x. root root Dec games
drwxr-xr-x. root root Dec include
drwxr-xr-x. root root Dec lib
drwxr-xr-x. root root Dec : lib64
drwxr-xr-x. root root Dec libexec
drwxr-xr-x. wheel Mar : mysql
-rwxr--r--. root root Jun : mysql--linux-glibc2-x86_64.tar
-rw-r--r--. wheel Mar : mysql--linux-glibc2-x86_64.tar.gz
-rw-r--r--. wheel Mar : mysql-test--linux-glibc2-x86_64.tar.gz
drwxr-xr-x. root root Dec sbin
drwxr-xr-x. root root Dec : share
drwxr-xr-x. root root Dec src
[[email protected] local]$ chown -R mysql mysql/
[[email protected] local]$ chgrp -R mysql mysql/
[[email protected] local]$ cd mysql/
5.安装和初始化数据库
[[email protected] mysql]$ bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
-- :: [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
-- :: [WARNING] The bootstrap log isn't empty:
-- :: [WARNING] --T22::Z [Warning] --bootstrap is deprecated. Please consider using --initialize instead
--T22::Z [Warning] Changed limits: max_open_files: (requested )
--T22::Z [Warning] Changed limits: table_open_cache: (requested )
如果改变默认安装路径,则需要 1)/etc/my.cnf、/etc/init.d/mysqld中修改 basedir=’/apps/mysql’ datadir=’/apps/mysql/data’ 2)创建ln mkdir -p /usr/local/mysql/bin ln -s /apps/mysql/bin/mysqld /usr/local/mysql/bin/mysqld
[root@localhost mysql]$
[root@localhost mysql]$ cp -a ./support-files/my-default.cnf /etc/my.cnf
[root@localhost mysql]$ cp -a ./support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]$ cd bin/
[root@localhost bin]# ./mysqld_safe --user=mysql &
[]
[root@localhost bin]# 2016-06-01T22:27:09.708557Z mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
--T22::09.Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
[root@localhost bin]# /etc/init.d/mysqld restart
Shutting down MySQL..--T22::Z mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.localdomain.pid ended
SUCCESS!
Starting MySQL. SUCCESS!
[]+ Done ./mysqld_safe --user=mysql
[root@localhost bin]$
//设置开机启动
[root@localhost bin]$ chkconfig --level mysqld on
[root@localhost bin]$
6.初始化密码
mysql5.7会生成一个初始化密码,而在之前的版本首次登陆不需要登录。
[[email protected] bin]$ cat /root/.mysql_secret
# Password set for user '[email protected]' at -- ::
,xxxxxR5H9
[[email protected] bin]$./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: .
Copyright (c) , , 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> SET PASSWORD = PASSWORD('123456');
Query OK, rows affected, warning ( sec)
mysql> flush privileges;
Query OK, rows affected ( sec)
7.添加远程访问权限
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host, user from user;
+-----------+-----------+
| host | user |
+-----------+-----------+
| % | root |
| localhost | mysql.sys |
+-----------+-----------+
//一定要重启才会生效。
/etc/init.d/mysqld restart
遇到的问题:
找不到mysql.sock,mysql.sock丢失问题解决方法
连接Mysql时,机器上找不到mysql.sock文件你可以简单地通过重启服务器重新创建得到它。因为服务器在启动时重新创建它。
[[email protected] mysql-5.7.13]# ./bin/mysql_install_db --user=mysql
2016-06-26 02:47:09 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2016-06-26 02:47:09 [ERROR] The data directory needs to be specified.
解决:需要新建data目录,指定安装目录(在非默认目录时需要指定,默认路径:/usr/local/mysql,同时需要指定./support-files/mysql.server中的basedir和datadir为相应路径)。
[[email protected] mysql-5.7.13]# ./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql-5.7.13/ --datadir=/usr/local/mysql-5.7.13/data/
2016-06-26 02:48:47 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2016-06-26 02:49:19 [WARNING] The bootstrap log isn't empty:
2016-06-26 02:49:19 [WARNING] 2016-06-26T09:48:50.798592Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2016-06-26T09:48:50.898732Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2016-06-26T09:48:50.898778Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
以上报错均因未在mysql.server中指定自定义的路径。
若是还提示已经有data文件了 删除了它,新建即可