MySQL相關操作
MySQL相關操作
一.MySQL常用操作
1.安裝MySQL
進入目錄下載mysql安裝包並解壓
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# ls
mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar -zxvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
移動mysql到/usr/local/mysql
[root@localhost src]# mv mysql-5.6.43-linux-glibc2.12-x86_64 /usr/local/mysql
新增使用者
[root@localhost src]# useradd -s /sbin/nologin mysql
新建/data/mysql,更改所有者及所屬組
[root@localhost src]# cd /usr/local/mysql/
[root@localhost mysql]# mkdir -p /data/mysql
[root@localhost mysql]# chown -R mysql:mysql /data/mysql/
編譯
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
需要下載擴充套件源perl-Module-Install
編譯完成後測試是否成功
[root@xserver1 mysql]# echo $?
0
覆蓋my.cnf檔案
[root@xserver1 mysql]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆蓋"/etc/my.cnf"? y
複製mysql.server到init.d
[root@xserver1 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
修改mysqld許可權
[root@xserver1 mysql]# chmod 755 /etc/init.d/mysqld
修改mysqld檔案
[root@xserver1 mysql]# vi /etc/init.d/mysqld
...
datadir=/data/mysql //此目錄為初始化資料庫時定義的目錄
...
將指令碼加入系統服務項
[root@xserver1 mysql]# chkconfig --add mysqld
設定開機自啟
[root@xserver1 mysql]# chkconfig mysqld on
[root@xserver1 mysql]# service mysqld start
Starting MySQL.Logging to '/data/mysql/xserver1.err'.
....... SUCCESS!
檢視MySQL是否啟動
2.更改root密碼
原始碼安裝需要更改將指令碼命令加入環境變數中
方法一:
直接修改環境變數
[root@localhost mysql]# export PATH=$PATH:/usr/local/mysql/bin/
方法二:
將此段命令寫入環境變數中
使環境變數生效
2.1建立mysql密碼
建立MySQL的密碼
[root@localhost mysql]# mysqladmin -uroot password '123456'
Warning: Using a password on the command line interface can be insecure.
注:warning提示為螢幕輸入密碼不安全可以忽略
使用新建立密碼登入
[root@localhost mysql]# mysql -uroot -p123456
注:當密碼比較複雜時-p選項後可以加上’ ’單引號
2.2重置密碼
修改配置檔案
[root@localhost mysql]# vi /etc/my.cnf
...
[mysqld]
skip-grant //新增此行--忽略授權操作直接登入
...
重啟mysql
[root@localhost mysql]# /etc/init.d/mysqld restart
登入mysql
[root@localhost mysql]# mysql
切換到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> update user set password=password('wslinux') where user='root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
注:第一個password表示一個欄位,第二個password表示一個函式用來加密
從user表中查詢使用者名稱為root的密碼(已加密)
將修改的配置檔案刪除
重啟mysql
嘗試使用新密碼登入
3.連線mysql
3.1 輸入使用者名稱和密碼來連結本機,直接登入資料庫
[root@localhost mysql]# mysql -uroot -p'wslinux'
3.2連結其他ip遠端伺服器的MySQL
[root@localhost mysql]# mysql -uroot -p'wslinux' -h127.0.01 -P3306
3.3 使用通訊方式socket登入,只適合本機登陸(相當於直接登入)
[root@localhost mysql]# mysql -uroot -p'wslinux' -S/tmp/mysql.sock
3.4連結MySQL時進行操作命令
檢視MySQL庫列表
[root@localhost mysql]# mysql -uroot -p'wslinux' -e "show databases"
注:-e後接操作命令
4.mysql常用命令
查詢庫
mysql> show databases;
切換庫
mysql> use mysql;
檢視庫中表
mysql> show tables;
注:此表為MySQL庫中的表,此操作需要進入庫中操作
查看錶中欄位
mysql> desc user; //命令後跟表名
檢視建表語句(查看錶是如何建立的)
mysql> show create table user\G; //命令語句後跟表名
注:\G表示通過豎行顯示
檢視當前登入使用者 (使用ip登入時會看到使用者名稱)
mysql> select user();
檢視當前使用的資料庫
mysql> select database();
建立庫
mysql> create database ws; //想要建立的庫名
建立表(需要先切換到表中在建立)
mysql> use ws; create table t1(`id` int(4),`name` char(40));
注:上述命令可以分開進行也可以同時進行;第一個欄位叫id,指定格式為int最長是4;第二個欄位時name,格式為欄位最長為40;需要使用 ` `引號擴起來
查看錶內容
注:可以自定義字符集
刪除表
自定義字符集
注:刪除原來的表,在建立表的後面從新加上引擎和字符集
檢視當前資料庫版本
mysql> select version();
檢視資料庫狀態
mysql> show status;
檢視資料庫的各引數
mysql> show variables;
mysql>show variables like 'max_connect%';
注:第一段命令為檢視所有引數,第二段為檢視指定引數
修改引數(可以在my.cnf修改也可以在MySQL中修改)
修改連結數:
mysql> set global max_connect_errors=1000;
檢視佇列
mysql> show processlist;
mysql>show full processlist;
注:兩段命令的差距在於info的區別,第二段命令可以顯示全命令
5.mysql建立使用者以及授權
使每個使用者對某一個數據庫有許可權或者對某一個表有許可權,目的是為了為防止誤操作
進行授權
mysql> grant all on *.* to 'user1' identified by 'passwd'; //對所有的許可權,on表示在那個庫中進行授權,*.*表示對所有的庫進行授權,to表示對使用者或ip進行授權,認證方式為passwd即密碼登入
Query OK, 0 rows affected (0.00 sec)
mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'127.0.0.1' identified by 'passwd';
Query OK, 0 rows affected (0.00 sec)
表示意義:給予許可權為查詢更新插入,授權庫為db1開頭的庫都有此許可權,受理使用者為user2,指定ip登入,密碼為passwd
mysql> grant all on db1.* to 'user3'@'%' identified by 'passwd';
Query OK, 0 rows affected (0.00 sec)
表示意義:給予所有許可權,對所有ip進行授權,受理使用者為user3,密碼為passwd
注:mysql的命令歷史檔案不儲存grant命令,MySQL的命令歷史儲存在root目錄下
mysql的命令歷史檔案
檢視授權表(檢視root的指定授權表)
mysql> show grants;
檢視user2受理的許可權內容
mysql> show grants for //檢視指定使用者的授權表資訊
例項:
常用使用方法:當已知使用者,需要增加授權的ip,但不知道密碼時,只需要將查出來的grant表的內容從新做一遍即可
此時這個使用者通過133.1和133.2都可以進行訪問