1. 程式人生 > 其它 >linux centos7 安裝MySQL5.7

linux centos7 安裝MySQL5.7

1:下載

https://dev.mysql.com/downloads/mysql/5.7.html#downloads

2:解壓

tar -xvf mysql-5.7.26-linux-glibc2.12-x86_64.tar 

3:再移動並重命名一下

mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql

4:建立mysql使用者組和使用者並修改許可權

groupadd mysql
useradd -r -g mysql mysql

5:建立資料目錄

mkdir -p  /data/mysql              #建立目錄
chown mysql:mysql -R /data/mysql   #
賦予許可權

6:配置my.cnf

vim /etc/my.cnf

內容

[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true

7:初始化資料庫

進入mysql的bin目錄

cd /usr/local/mysql/bin/

初始化

./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize


檢視密碼

cat /data/mysql/mysql.err

8:啟動mysql,並更改root 密碼
先將mysql.server放置到/etc/init.d/mysql中

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

啟動!!!

service mysql start

如果以前安裝過,沒有解除安裝乾淨,會遇到一下問題

[root@VM-0-4-centos bin]# service mysql start
Starting MySQL... ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).

檢視日誌,埠在佔用

解決

[root@VM-0-4-centos ~]# netstat -apn  | grep 3306 
tcp6       0      0 :::3306                 :::*                    LISTEN      12519/mysqld        
[root@VM-0-4-centos ~]# kill -9 12519
[root@VM-0-4-centos ~]# ^C

重新啟動,就OK

[root@VM-0-4-centos mysql]# service mysql start
Starting MySQL. SUCCESS! 
[root@VM-0-4-centos mysql]# 

首先登入mysql,密碼是前面隨機生成的。

[root@VM-0-4-centos mysql]# cd /usr/local/mysql/
[root@VM-0-4-centos mysql]# ./bin/my
myisamchk                   my_print_defaults           mysqlcheck                  mysqld                      mysqldump                   mysql_install_db            mysqlshow                   mysql_tzinfo_to_sql
myisam_ftdump               mysql                       mysql_client_test_embedded  mysqld-debug                mysqldumpslow               mysql_plugin                mysqlslap                   mysql_upgrade
myisamlog                   mysqladmin                  mysql_config                mysqld_multi                mysql_embedded              mysqlpump                   mysql_ssl_rsa_setup         mysqlxtest
myisampack                  mysqlbinlog                 mysql_config_editor         mysqld_safe                 mysqlimport                 mysql_secure_installation   mysqltest_embedded          
[root@VM-0-4-centos 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>

重置密碼

mysql> 
mysql> SET PASSWORD = PASSWORD('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;  
Query OK, 0 rows affected (0.00 sec)

mysql>

允許遠端連線

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> 
mysql> update user set host = '%' where user = 'root'; 
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> 
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql>

如果不希望每次都到bin目錄下使用mysql命令則可以建立軟連線

ln -s  /usr/local/mysql/bin/mysql    /usr/bin

至此,完成!