我用的非root账户 在 /usr/local目录下安装
卸载系统自带的 mariadb 否则影响 mysql 的安装
查找是否有:rpm -qa|grep mariadb
卸载:sudo rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
创建 mysql 用户组: sudo groupadd mysql
创建 mysql 用户(禁止登录): sudo useradd -r -g mysql -s /bin/false mysql
切换至 mysql 安装目录: cd /usr/local
[qgn@localhost ~]$ rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[qgn@localhost ~]$ sudo rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
[qgn@localhost ~]$ sudo groupadd mysql
[qgn@localhost ~]$ sudo useradd -r -g mysql -s /bin/false mysql
[qgn@localhost ~]$ cd /usr/local
[qgn@localhost local]$
下载 MySQL
下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
点击 DownLoad
不登录下载即可
或者直接在linux 上下载
sudo wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
解压 sudo tar zxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
修改文件夹名字为 mysql:sudo mv mysql-5.7.26-linux-glibc2.12-x86_64 mysql
在 mysql 创建 data 文件夹:sudo mkdir mysql/data
更改文件所属用户和组:sudo chown -R mysql:mysql ./mysql
[qgn@localhost local]$ sudo mv ~/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz ./
[qgn@localhost local]$ sudo tar zxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[qgn@localhost local]$ sudo mv mysql-5.7.26-linux-glibc2.12-x86_64 mysql
[qgn@localhost local]$ sudo mkdir mysql/data
[qgn@localhost local]$ sudo chown -R mysql:mysql ./mysql
[qgn@localhost local]$ ll
总用量 629756
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 3 root root 26 5月 22 20:41 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
drwxr-xr-x. 10 mysql mysql 141 5月 25 11:18 mysql
-rw-rw-r--. 1 qgn qgn 644869837 5月 25 11:12 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 5月 23 04:09 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
[qgn@localhost local]$
进入 mysql 目录:cd mysql
初始化: ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
如果遇到下面错误。则是权限不够,更改 data 文件夹的权限:sudo chmod 777 data
然后再初始化,完成之后,绿色方框里面 root@localhost 后面为初始密码,记下来,一会登录用
将 support-files下面的mysql.server 文件复制到 /etc/init.d/mysql sudo cp support-files/mysql.server /etc/init.d/mysql
修改 /etc/init.d/mysql 文件如下: sudo vim /etc/init.d/mysql
更改 /etc/init.d/mysql 文件执行权限: sudo chmod a+x /etc/init.d/mysql
直接启动:/etc/init.d/mysql start 或者 service mysql start
[qgn@localhost mysql]$ sudo cp support-files/mysql.server /etc/init.d/mysql
[qgn@localhost mysql]$ sudo vim /etc/init.d/mysql
[qgn@localhost mysql]$ sudo chmod a+x /etc/init.d/mysql
[qgn@localhost mysql]$ /etc/init.d/mysql start
Starting MySQL. SUCCESS!
[qgn@localhost mysql]$
成功开启 mysql 服务
查看 mysql 服务 ps -ef | grep mysql
[qgn@localhost mysql]$ ps -ef | grep mysql
qgn 78467 1 0 17:14 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/localhost.localdomain.pid
qgn 78558 78467 0 17:14 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=localhost.localdomain.err --pid-file=/usr/local/mysql/data/localhost.localdomain.pid
qgn 79018 20835 0 17:15 pts/0 00:00:00 grep --color=auto mysql
[qgn@localhost mysql]$
开启时遇到下面错误
Starting MySQL./usr/local/mysql/bin/mysqld_safe:行647: /var/log/mariadb/mariadb.log: 没有那个文件或目录
2019-05-22T15:51:02.864835Z mysqld_safe error: log-error set to ‘/var/log/mariadb/mariadb.log’, however file don’t exists. Create writable for user ‘mysql’.
/usr/local/mysql/bin/mysqld_safe:行144: /var/log/mariadb/mariadb.log: 没有那个文件或目录
ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).
解决办法:
创建文件夹 创建文件
sudo mkdir /var/log/mariadb
sudo touch /var/log/mariadb/mariadb.log
更改文件权限
sudo chown mysql:mysql mariadb.log
sudo chmod 666 mariadb.log登陆 mysql bin/mysql -uroot -p
输入初始化时记下的密码
进入mysql进行操作则会提示更改密码,
将密码更改为 mysql
mysql> set password=password('mysql');
查看用户以及权限
mysql> use mysql;
mysql> select host,user from user;
更改权限表,使 mysql 可以远程连接
mysql> update user set host = '%' where user ='root';
重新加载权限表
mysql> flush privileges;
[qgn@localhost mysql]$ bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26
Copyright (c) 2000, 2019, 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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> set password=password('mysql');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
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> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> update user set host = '%' where user ='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> quit;
Bye
配置开机启动
添加为服务: chkconfig --add /etc/init.d/mysql
查看服务列表: chkconfig --list
看到3、4、5状态为开或者为 on 则表示成功。如果是 关或者 off 则执行一下:chkconfig --level 345 mysqld on
重启计算机:reboot
再次查看服务列表或者查看3306端口号