搜尋
首頁資料庫mysql教程一文總結Mysql資料庫應用及常見問題

一、安裝mysql

1、CentOS系統

1.)安裝mysql

#下載並安裝mysql的repo來源
$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
安裝mysql
$ sudo yum install -y mysql-server

2.)啟動/重新啟動/關閉服務

service mysqld start | restart | stop

2、alpine系統

alpine系統中安裝mysql實際上是開源的MariaDB,MariaDB資料庫是MySQL的一個分支/衍生版,完全相容MySQL,並在擴充功能、儲存引擎以及一些新的功能改進方面都強過MySQL,安裝參考
$ apk update

# 安装数据库及客户端
$ apk add mysql mysql-client

# 初始化数据库
$ mysql_install_db --user=mysql --datadir=/var/lib/mysql

# 启动服务
$ rc-service mariadb start  # 若没有rc,安装:apk add openrc

# 修改密码
$ mysqladmin -u root password '新root密码'

# 加入开机启动
$ rc-update add mariadb default

二、操作mysql

1、登入資料庫

預設初始化直接登入
$ mysql
若提示失敗,說明有密碼,運行:
$ mysql -uroot -p

# 一次性登录
$ mysql -u用户 -p密码

2、操作資料庫

# 查看所有数据库
mysql> show databases;

# 创建数据库
mysql> create database xxx charset=utf8;

# 删除数据库
mysql> drop database xxx;

# 切换数据库
mysql> use mysql;

# 查看表
mysql> show tables;

# 创建表
mysql> create table xxx (
    id int,
    name varchar(20),
    update_time datetime
);

# 删除表
mysql> drop table xxx;

# 显示表结构
mysql> desc xxx;

# 查询指定a,b字段的记录,不知道用*代替
mysql> select a,b form xxx;

# 插入记录
mysql> insert into xxx(id,user) values(1,'wang',now());

# 删除记录
mysql> delete from xxx where name='wang';

# 清屏命令
mysql> system clear;

# 退出
mysql> quit;

3、使用者管理

mysql的user表用來儲存所有使用者權限,其中host欄位表示指定ip用戶能使用,同名不同host的為兩個用戶,host常見值如下:

localhost:只能在伺服器端使用 
192.168.4.%:指定ip段能用 
% :通配符,表示所有ip用戶都能用,多用於遠端連線

下面新建/刪除使用者操作時,可透過user@'host'指定,若不指定預設為%

# 切换到mysql权限数据库
mysql> use mysql;

# 查看用户及所属的host
mysql> select user,host from user;

# 新建用户并设置密码
mysql> create user '用户名' identified by '密码';

# 上面不指定host,默认%,等同于:
mysql> create user '用户名'@'%' identified by '密码';

# 删除用户(只删除host为%的用户)
mysql> drop user '用户名';

# 查看用户权限
mysql> show grants for '用户名';

# 设置权限,并指定数据库
mysql> grant all privileges on xxxDB.* to '用户名';

# 修改密码,注意密码处不能直接password='新密码'
mysql> update user set password=password('新密码') where user='用户名';

# 刷新权限表
mysql> flush privileges;

4、批次操作

1.)sql腳本批次執行

$ mysql -uroot -p -Dxxx <p><strong>2.)備份/還原資料庫</strong></p><pre class="brush:php;toolbar:false">$ mysqldump --all-databases -h127.0.0.1 -u root -p > ./backup/mysql-bak.sql
$ mysqldump --all-databases -h127.0.0.1 -u root -p <h2 id="三-常見問題">三、常見問題</h2><h4 id="root密碼忘記怎麼擦除">1、root密碼忘記怎麼擦除? </h4><pre class="brush:php;toolbar:false">$ mysql –skip-grant-table &
  mysql> use mysql;
  mysql> update user set password=password('新密码') where user='root';
  mysql> flush privileges;
  mysql> quit;
這裡要注意的是每句指令需要用分號「;」結尾,執行完以上得操作,root的密碼就被清空。

2、空白用戶錯誤

ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'錯誤:

#原因:

mysql資料庫的user表裡,有使用者名稱為空的帳戶即匿名帳戶,導致登入的時候雖然用的是root,但實際上是匿名登入的。

處理方案:

# 1.关闭mysql
$ service mysqld stop

# 2.屏蔽权限
$ mysqld_safe --skip-grant-table # 屏幕出现: Starting demo from .....

# 3.新开起一个终端输入
$ mysql -uroot mysql
  mysql> update user set password=password('新密码') where user='root';
  mysql> flush privileges;
  mysql> quit;

3、服務上root能用,遠端無法登入

$ mysql -uroot -p
  mysql> use mysql;
  
  # 先查看user表host字段,有无通配符'%',若有直接运行flush privileges;
  mysql> select host from user where user='root';  
  mysql> grant all privileges *.* to 'root'@'%' identified by 'root密码';
  mysql> flush privileges;
  mysql> quit;

4、遠端連線提示caching_sha2_password錯誤

從mysql5.7版本之後,預設採用了caching_sha2_password驗證方式
mysql> use mysql;
mysql> alter user 'root'@'%' identified with mysql_native_password by 'root密码';

5、mysql修改預設資料存放

$ mysqladmin -u root -p variables | grep datadir  # 查看mysql数据库存放目录
$ service mysqld stop
$ mv /var/lib/mysql /路径  # 移动数据库文件

$ vi /etc/my.cnf

# 修改datadir和socket两个字段,并添加以下:
[mysql] 
socket=/路径/mysql.sock

$ service mysqld start

推薦學習:《mysql影片教學

#

以上是一文總結Mysql資料庫應用及常見問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:segmentfault。如有侵權,請聯絡admin@php.cn刪除
在MySQL中使用視圖的局限性是什麼?在MySQL中使用視圖的局限性是什麼?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他們不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinsOrsubqueries.2)他們canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

確保您的MySQL數據庫:添加用戶並授予特權確保您的MySQL數據庫:添加用戶並授予特權May 14, 2025 am 12:09 AM

porthusermanagementinmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素會影響我可以在MySQL中使用的觸發器數量?哪些因素會影響我可以在MySQL中使用的觸發器數量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)複雜的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存儲斑點安全嗎?mysql:存儲斑點安全嗎?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通過PHP Web界面添加用戶mySQL:通過PHP Web界面添加用戶May 14, 2025 am 12:04 AM

通過PHP網頁界面添加MySQL用戶可以使用MySQLi擴展。步驟如下:1.連接MySQL數據庫,使用MySQLi擴展。 2.創建用戶,使用CREATEUSER語句,並使用PASSWORD()函數加密密碼。 3.防止SQL注入,使用mysqli_real_escape_string()函數處理用戶輸入。 4.為新用戶分配權限,使用GRANT語句。

mysql:blob和其他無-SQL存儲,有什麼區別?mysql:blob和其他無-SQL存儲,有什麼區別?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而ilenosqloptionslikemongodb,redis和calablesolutionsolutionsolutionsoluntionsoluntionsolundortionsolunsonstructureddata.blobobobissimplobisslowdeperformberbutslowderformandperformancewithlararengedata;

mySQL添加用戶:語法,選項和安全性最佳實踐mySQL添加用戶:語法,選項和安全性最佳實踐May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串數據類型常見錯誤?MySQL:如何避免字符串數據類型常見錯誤?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingSefectery.1)usecharforfixed lengengtrings,varchar forvariable-varchar forbariaible length,andtext/blobforlargerdataa.2 seterters seterters seterters

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。