#卸載系統自帶的Mariadb
[root@master ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
[root@master ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
#檢查mysql是否存在
[root@master ~]# rpm -qa | grep mysql
[root@master ~]#
#下載MySQL壓縮包
#解壓並並改名爲mysql,在此文件夾下新建data目錄
[root@master apps]# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@master apps]# mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql
[root@master apps]# cd mysql
[root@master apps]# mkdir data
#在etc下新建配置文件my.cnf,並在該文件內添加以下配置
[root@master ~]#vi /etc/my.cnf
[mysql]
#設置mysql客戶端默認字符集
default-character-set=utf8
[mysqld]
skip-name-resolve
#設置3306端口
port = 3306
# 設置mysql的安裝目錄
basedir=/apps/mysql/
# 設置mysql數據庫的數據的存放目錄
datadir=/apps/mysql/data
# 允許最大連接數
max_connections=200
# 服務端使用的字符集默認爲8比特編碼的latin1字符集
character-set-server=utf8
# 創建新表時將使用的默認存儲引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
log-error = /apps/mysql/data/error.log
pid-file = /apps/mysql/data/mysql.pid
user = root
tmpdir = /tmp
socket = /tmp/mysql.sock
#安裝和初始化
[root@master ~]cd /apps/mysql
[root@master mysql]bin/mysql_install_db --user=root --basedir=/apps/mysql --datadir=/apps/mysql/data
[root@master mysql]cp ./support-files/mysql.server /etc/init.d/mysqld
[root@master mysql]chown 777 /etc/my.cnf
[root@master mysql]chmod +x /etc/init.d/mysqld
#啓動服務
[root@master mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
#查看端口運行情況(3306)
[root@master mysql]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1326/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 983/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 986/cupsd
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 983/sshd
tcp6 0 0 ::1:631 :::* LISTEN 986/cupsd
tcp6 0 0 :::3306 :::* LISTEN 16421/mysqld
#登錄MySQL
#查看生成的初始密碼
[root@master mysql]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2018-07-06 10:36:35
#Lws4rXsnhbA
#密碼登錄(可能會報錯)
[root@master mysql]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
#如果登錄成功,顯示成這樣
[root@master ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
============================================================================#安裝過程中可能出現的錯誤
錯誤1:解決嚴格按照下面配置進行(/tmp/mysql.sock)位置至關重要
ERROR! The server quit without updating PID file
[root@master mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/apps/mysql/data/master.hadoop.err'.
ERROR! The server quit without updating PID file (/apps/mysql/data/master.hadoop.pid)
[mysql]
# 設置mysql客戶端默認字符集
default-character-set=utf8
[mysqld]
skip-name-resolve
#設置3306端口
port = 3306
# 設置mysql的安裝目錄
basedir=/apps/mysql/
# 設置mysql數據庫的數據的存放目錄
datadir=/apps/mysql/data
# 允許最大連接數
max_connections=200
# 服務端使用的字符集默認爲8比特編碼的latin1字符集
character-set-server=utf8
# 創建新表時將使用的默認存儲引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
log-error = /apps/mysql/data/error.log
pid-file = /apps/mysql/data/mysql.pid
user = root
tmpdir = /tmp
socket = /tmp/mysql.sock
然後:刪除data目錄中的所有文件,重新初始化
[root@master mysql]# rm -rf data/*
[root@master mysql]# bin/mysql_install_db --user=mysql --basedir=/apps/mysql/ --datadir=/apps/mysql/data/
錯誤2:(mysql_install_db: [ERROR] Found option without preceding group in config file /etc/my.cnf at line 1!)
[root@master mysql]# bin/mysql_install_db --user=mysql --basedir=/apps/mysql/ --datadir=/apps/mysql/data/
mysql_install_db: [ERROR] Found option without preceding group in config file /etc/my.cnf at line 1!
mysql_install_db: [ERROR] Fatal error in defaults handling. Program aborted!
[root@master mysql]# vi /etc/my.cnf(該文件有語法錯誤,修改)
============================================================================
#如果再登錄中出現了問題
#查看生成的初始密碼
[root@master mysql]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2018-07-06 10:36:35
#Lws4rXsnhbA
#密碼登錄(可能會報錯)
[root@master mysql]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
解決辦法:
[root@master mysql]# service mysqld stop
Shutting down MySQL.. SUCCESS!
#報錯解決方法
[root@master mysql]# vi /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables 保存並且退出vi。
[root@master mysql]# service mysqld start
Starting MySQL. SUCCESS!
[root@master mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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的root密碼
mysql> USE mysql ;
mysql> update user set password=password("*******") where user="*******"; #修改密碼報錯
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql> update mysql.user set authentication_string=password('*******') where user='*******'; #修改密碼成功
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges ;
mysql> exit
# vi /etc/my.cnf
將剛纔在[mysqld]的段中加上的skip-grant-tables刪除
保存並且退出vi。
重新啓動mysqld
登陸後重要的一步
否則報錯:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
解決辦法:
安裝完mysql 之後,登陸以後,不管運行任何命令,總是提示這個
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
step 1: SET PASSWORD = PASSWORD('your new password');
step 2: ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
step 3: flush privileges;
完成以上三步退出再登,使用新設置的密碼就行了,後兩部原樣設置即可
mysql> SET PASSWORD = PASSWORD('******');
Query OK, 0 rows affected, 1 warning (0.00 sec)
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.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> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
Bye
[root@master ~]#