一,Yum安裝mysql
1,使用官方的yum源(MySQL Yum Repository)
-
wget https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
Paste_Image.png 安裝yum的源 yum localinstall mysql57-community-release-el7-9.noarch.rpm//解決本地rpm包依賴的問題。
image.png
yum repolist all | grep mysql //查看可用的mysql源
yum-config-manager --disable mysql55-community
yum-config-manager --disable mysql56-community
yum-config-manager --enable mysql57-community啟用哪個版本的mysql源
yum repolist enabled | grep mysql查看啟動的源
使用yum安裝指定版本的mysql
yum -q list available --showduplicates mysql-community-server.x86_64
yum install mysql-community-server-5.7.12-1.el6.x86_64
mysql --version
安裝mysql服務 yum install mysql-community-server
啟動mysql服務器(server)
service mysqld start //centos 7 以下
systemctl start mysqld //centos 7
5)查看mysql默認密碼。
sudo grep 'temporary password' /var/log/mysqld.log(密碼可以直接復制,輸入終端)
命令行執行, mysql_secure_installation,可以進行mysql的安全設置,也可修改密碼(不需要登錄mysql)。
mysql -uroot -p登錄(使用默認密碼)
SHOW VARIABLES LIKE 'validate_password%';
SET PASSWORD = PASSWORD('new_password');//修改成符合規則的密碼。
set global validate_password_policy=0;采用只驗證長度的規則
set global validate_password_length=6;修改密碼最小長度
show global variables like "%super_read_only%";// 全局只讀
flush privileges;
- 忘記、修改密碼
關閉mysql服務。systemctl stop mysqld
/etc/my.cnf 增加參數skip-grant-tables #跳過授權, 使用這個參數的時候,需要注釋掉validate_password配置
validate_password=OFF #不使用密碼加強插件
登錄mysql mysql -uroot
選擇數據庫 use mysql
修改密碼 update user set authentication_string=PASSWORD('pwd!@#') where User='root';
刷新 flush privileges;
重啟mysql。systemctl restart mysqld
2, 查看服務器的狀態
status是狀態是系統的狀態不可更改,是系統現在的運行狀態參數。
variables是系統變量。全局變量影響服務器的全局操作。會話變量影響具體客戶端連接相關操作。可以使用set global max_connections = 1000
1)show variables; 查看mysql服務器的配置信息。
show variables like "%max_connections%"; // 最大倆結束
show global variables like "%datadir%"; //數據文件的目錄
show processlist // 查看當前連接的信息。
2)show (global) status; 查看mysql服務器的狀態信息(本次mysql啟動后)
show global status like "com_insert"; // 執行insert的總次數。
show global status like "Com_select"; //執行的select總次數。
show global status like "uptime"; //運行時間。
3)使用socket客戶端登錄。(不需要重啟)
[client]
host = 127.0.0.1
user = root
password = root
port = 3306
socket = /var/nebula/mysql/mysql.sock
3,非本機連接數據庫
1)阿里云機器需要增加安全組。端口3306
2)允許執行ip,用戶名連接mysql服務器。
問題:
命令
解決方案:
給ip以及用戶授權。
grant all privileges on *.* to 'root'@'119.57.xx.xx' identified by 'pwdxxx';
flush privileges;
不停止服務修改密碼
grant all privileges on *.* to root@localhost identified by 'pwd';
grant all privileges on *.* to root@127.0.0.1 identified by 'pwd';
flush privileges;
查看現有用戶以及權限
select user, host from mysql.user;
show grants for 'root'@'%';
授予權限
GRANT SELECT ON \ *.* TO 'reader'@'%' IDENTIFIED BY 'reader';
GRANT ALL ON *.* TO 'write'@'%' IDENTIFIED BY 'write' with grant option;
取消授權
drop user root@"172.17.0.1";
執行mysql遠程登錄即可。
查看mysql status
.show status;
show global status;
6,直接使用/usr/local/mysql目錄直接安裝mysql
1)拷貝一份
image.png
2)/var/lib/mysql/ 清空原有內容
3)在my.cnf增加skip-grant-tables
/usr/local/mysql/sbin/mysqld --initialize
/usr/local/mysql/support-files/mysql.server -->/etc/init.d/mysqld啟動腳本
update user set authentication_string=PASSWORD('root') where User='root';
flush privileges;
清空/var/lib/mysql/
復制一份 /usr/local/mysql/support-files/my-default.cnf -> /etc/my.cnf,增加skip-grant-tables
/usr/local/mysql/sbin/mysqld --initialize
/usr/local/mysql/support-files/mysql.server -->/etc/init.d/mysqld啟動腳本
update user set authentication_string=PASSWORD('root') where User='root';
flush privileges;