mysql -u 用戶名 -p 用戶密碼html
use mysql;mysql
update user set password=password('11111111') where user='root' and host='localhost';web
flush privileges;sql
MariaDB [mysql]> update user set password=password('11111111') where user='root' and host='localhost'; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [mysql]> flush privileges; Query OK, 0 rows affected (0.01 sec) MariaDB [mysql]> exit
insert into mysql.user(host,user,password)values("localhost","test",password("password"));數據庫
flush privileges;centos
DELETE FROM user WHERE User="test" and Host="localhost";
flush privileges; 緩存
drop database test1;服務器
mysql_secure_installation網絡
show databases:查看全部的數據庫併發
create database zxg:建立名尾zxg的數據庫
use zxg:進入zxg的數據庫
show tables:查看數據庫裏有多少張表
create table t1 (id varchar(20),name varchar(20)):建立名爲t1表,並建立兩兩個字段,id、name,varchar表示設置數據長度,用字符來定義長度單位,其
insert into t1 values("1","zxg"):向表中插入數據
select * from t1 :查看t1表數據內容
select * from t1 where id=1 and age = 'zxg ' : id、age 多個條件查詢
desc t1:查看t1 表字段內容
alter table t1 modify column name varchar(20):修改name字段的長度
update t1 set name='zxg.net' where id=1:修改name字段的內容
flush privileges :刷新權限
delete from t1 :清空表內容
drop table t1:刪除表
drop database zxg:刪除zxg數據庫
show variables like '%char%':查看數據庫字符集
show engines:查看MySQL存儲引擎。
show variables like '%storage_engine%':查看MySQL默認的存儲引擎
alter table t1 engine=innodb:修改MySQL t1表存儲引擎
1 [root@web2 ~]# mysql 2 Welcome to the MariaDB monitor. Commands end with ; or \g. 3 Your MariaDB connection id is 2 4 Server version: 5.5.60-MariaDB MariaDB Server 5 6 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. 7 8 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 9 10 MariaDB [(none)]> show databases; 11 +--------------------+ 12 | Database | 13 +--------------------+ 14 | information_schema | 15 | mysql | 16 | performance_schema | 17 | test | 18 +--------------------+ 19 4 rows in set (0.00 sec) 20 21 MariaDB [(none)]> create database zxg; 22 Query OK, 1 row affected (0.00 sec) 23 24 MariaDB [(none)]> use zxg; 25 Database changed 26 MariaDB [zxg]> show tables; 27 Empty set (0.00 sec) 28 29 MariaDB [zxg]> create table t1(id varchar(20),name varchar(20)); 30 Query OK, 0 rows affected (0.00 sec) 31 32 MariaDB [zxg]> show tables; 33 +---------------+ 34 | Tables_in_zxg | 35 +---------------+ 36 | t1 | 37 +---------------+ 38 1 row in set (0.00 sec) 39 40 MariaDB [zxg]>
1 MariaDB [zxg]> insert into t1 values ("1","zxg"); 2 Query OK, 1 row affected (0.01 sec) 3 4 MariaDB [zxg]> select *from t1; 5 +------+------+ 6 | id | name | 7 +------+------+ 8 | 1 | zxg | 9 +------+------+ 10 1 row in set (0.00 sec) 11 12 MariaDB [zxg]> select *from t1 where id=1; 13 +------+------+ 14 | id | name | 15 +------+------+ 16 | 1 | zxg | 17 +------+------+ 18 1 row in set (0.00 sec) 19 20 MariaDB [zxg]> select *from t1 where id=1 and name='zxg'; 21 +------+------+ 22 | id | name | 23 +------+------+ 24 | 1 | zxg | 25 +------+------+ 26 1 row in set (0.01 sec) 27 28 MariaDB [zxg]> desc t1; 29 +-------+-------------+------+-----+---------+-------+ 30 | Field | Type | Null | Key | Default | Extra | 31 +-------+-------------+------+-----+---------+-------+ 32 | id | varchar(20) | YES | | NULL | | 33 | name | varchar(20) | YES | | NULL | | 34 +-------+-------------+------+-----+---------+-------+ 35 2 rows in set (0.00 sec) 36 37 MariaDB [zxg]> alter table t1 modify column name varchar(20); 38 Query OK, 0 rows affected (0.00 sec) 39 Records: 0 Duplicates: 0 Warnings: 0 40 41 MariaDB [zxg]> update t1 set name='zxg.net' where id=1; 42 Query OK, 1 row affected (0.00 sec) 43 Rows matched: 1 Changed: 1 Warnings: 0 44 45 MariaDB [zxg]>
mysql數據庫存儲數據時,默認編碼爲latinl,存儲中文字符時,在調用時會顯示爲亂碼,爲了解決該亂碼問題,需修改該mysql默認字符集爲UTE-8
裝mariadb的是時候看已經默認爲ute-8
MariaDB [(none)]> show variables like '%char%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
如不是,能夠設置
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
設置密碼訪問,密碼破解、密碼權限、修改密碼;
grant all on zxg.* to test@localhost identified by 'pas';
grant select,insert,update,delete on *.*to test@"%" identified by 'pas';
grant all on zxg.* to test@`192.168.216.53` identified by 'pas'
中止服務---》跳過權限方式啓動---》單開一個窗口登錄---》登錄修改密碼便可
1)systemctl stop mariadb
2)mysqld_safe --skip-grant-tables &
[root@web2 ~]# mysqld_safe --skip-grant-tables & [1] 47542 [root@web2 ~]# 190520 15:45:22 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'. 190520 15:45:22 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@web2 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 1 Server version: 5.5.60-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> 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 MariaDB [mysql]> update user set password=password('11111111') where user='root' ; Query OK, 3 rows affected (0.01 sec) Rows matched: 4 Changed: 3 Warnings: 0 MariaDB [mysql]> flush privileges; Query OK, 0 rows affected (0.00 sec) MariaDB [mysql]> exit Bye [root@web2 ~]#
而後退出」跳過權限方式「 ctrl+c ,正常啓動mysql就能夠了
[mysqld] #服務端配置 port = 3306 #監聽端口 socket = /tmp/mysql.sock #通訊設置 user = mariadb #使用mariadb用戶啓動 basedir = /usr/local/mariadb #安裝路徑 datadir = /data/mysql #數據目錄 log_error = /data/mysql/mariadb.err #錯誤日誌 pid-file = /data/mysql/mariadb.pid #pid進程文件 skip-external-locking #避免mysql的外部鎖定,減小出錯概率提升穩定性 key_buffer_size = 64M #緩存存儲引擎參數,這個參數能夠設置爲64M max_allowed_packet = 1M #容許最大接收數據包的大小,防止服務器發送過大的數據包,能夠設置爲16MB或者更大,但設置太大也可能有危險 table_open_cache = 256 #mysql每打開一個表,都會讀入一些數據到table_open_cache緩存中,當MYSQL在這個緩存中找不到相應的信息時,纔會去磁盤讀取,默認值64,假設系統有200個併發鏈接,則需將此參數設置爲200*N(N爲每一個鏈接所需的文件描述符數目);當設置爲很大時,若是系統處理不了那麼多文件描述符,那麼就會出現客戶端失效,鏈接不上 sort_buffer_size = 1M #在表進行order by和group by 排序操做時,因爲排序的字段沒有索引,會出現Using filesort,爲了提升性能,可用此參數增長每一個線程分配的緩存區大小,默認時256k,這個參數不要設置過大,通常128~256k,另一般出現using filesort的時候,要經過增長索引來解決 net_buffer_length = 8K #包消息緩衝區初始化net_buffer_length字節,但須要時能夠增加到max_allowed_packet字節 read_buffer_size = 1M #該參數用於表的順序掃描,表示每一個線程分配的緩衝區大小,好比在進行全表掃描時,mysql會按照數據的存儲順序一次讀取數據塊,每次讀取的數據塊首先會暫存在read_buffer_size中,當buffer空間被寫滿或者所有數據讀取結束後,在將buffer中的數據返回給上層調用者,以提升效率默認128k,也不要設置過大 read_rnd_buffer_size = 512K #該參數用於表的隨機讀取,表示每一個線程分配的緩衝區大小,好比,按照一個非索引字段作order by排序操做時,就會利用這個緩衝區來暫存讀取的數據,默認時256k,也不要設置過大 myisam_sort_buffer_size = 16M #當myisam表執行repair table或建立索引時,用以緩衝排序索引,設置過小可能會遇到"myisam_sort_buffer_size is to small" thread_cache_size = 32 #線程池,線程緩衝。用來緩衝空閒的線程,以致於不被銷燬,若是線程緩衝在的空閒線程,須要從新創建新鏈接,則會優先調用線程池中的緩衝,很快就能相應鏈接請求,每創建一個鏈接,都須要一個線程與之匹配。 query_cache_size = 32M #緩存select語句和結果集大小的參數。查詢緩存會存儲一個select查詢的文本與被傳送到客戶端的相應結果。若是以後接收到一個相同的查詢,服務器會從查詢緩存中檢索結果,而不是再次分析和執行這個一樣的查詢。若是你的環境中寫操做不多,讀操做頻繁,那麼打開query_cache_type=1,會對性能有明顯提高。若是寫操做頻繁,則應該關閉它(query_cache_type=0)。 tmp_table_size = 64M #臨時HEAP數據表的最大長度(默認設置是32M); 超過這個長度的臨時數據表將被轉換爲MyISAM數據表並存入一個臨時文件。 # explicit_defaults_for_timestamp = true #是否顯示默認時間戳 #skip-networking # max_connections = 500 #該參數用來設置最大鏈接數,告訴你當前你的服務器容許多少併發鏈接。默認爲100,通常設置爲512-1000便可。請記住,太多的鏈接會致使內存的使用量太高而且會鎖住你的 MySQL 服務器。通常小網站須要 100-200 的鏈接數,而較大可能須要 500-800 甚至更多。這裏的值很大程度上取決於你 MySQL/MariaDB 的使用狀況。 max_connect_errors = 100 #若是有時網絡抽風,或者應用配置錯誤,或者其餘緣由致使客戶端短期內不斷的嘗試鏈接,客戶端可能會被列入黑名單,而後將沒法鏈接,直到再次刷新主機緩存。這個選項默認值過小了,能夠考慮設的足夠大(若是你的服務器配置夠強大的話)。 open_files_limit = 65535 #mysql打開最大文件數 # log-bin=mysql-bin #這些路徑相對於datadir binlog_format=mixed #日誌格式 server-id = 1 #給服務器分配一個獨一無二的ID編號; n的取值範圍是1~2的32次方啓用二進制日誌功能。在複製數據同步的時候會用到,Helloweba後面會有文章介紹。 expire_logs_days = 10 #啓用二進制日誌後,保留日誌的天數。服務器會自動清理指定天數前的日誌文件,若是不設置則會致使服務器空間耗盡。通常設置爲7~14天。 # default_storage_engine = InnoDB #新數據表的默認存儲引擎(默認設置是MyISAM)。這項設置還能夠經過–default-table-type選項來設置。 innodb_file_per_table = 1 #提供了更靈活的方式,它把每一個數據庫的信息保存在一個 .ibd 數據文件中。每一個 .idb 文件表明它本身的表空間。經過這樣的方式能夠更快地完成相似 「TRUNCATE」 的數據庫操做,當刪除或截斷一個數據庫表時,你也能夠回收未使用的空間。這樣配置的另外一個好處是你能夠將某些數據庫表放在一個單獨的存儲設備。這能夠大大提高你磁盤的 I/O 負載。 innodb_data_home_dir = /data/mysql #InnoDB主目錄,全部與InnoDB數據表有關的目錄或文件路徑都相對於這個路徑。在默認的狀況下,這個主目錄就是MySQL的數據目錄。 innodb_data_file_path = ibdata1:10M:autoextend #用來容納InnoDB爲數據表的表空間: 可能涉及一個以上的文件; 每個表空間文件的最大長度都必須以字節(B)、兆字節(MB)或千兆字節(GB)爲單位給出; 表空間文件的名字必須以分號隔開; 最後一個表空間文件還能夠帶一個autoextend屬性和一個最大長度(max:n)。 innodb_log_group_home_dir = /data/mysql #用來存放InnoDB日誌文件的目錄路徑(如ib_logfile0、ib_logfile1等)。在默認的狀況下,InnoDB驅動程序將使用 MySQL數據目錄做爲本身保存日誌文件的位置。 innodb_buffer_pool_size = 256M #這個參數是InnoDB存儲引擎的核心參數,默認爲128KB,這個參數要設置爲物理內存的60%~70%。 innodb_log_file_size = 64M #事務日誌文件寫操做緩存區的最大長度(默認設置是1MB)。 innodb_log_buffer_size = 8M #事務日誌所使用的緩存區。InnoDB在寫事務日誌的時候,爲了提升性能,先將信息寫入Innodb Log Buffer中,當知足innodb_flush_log_trx_commit參數所設置的相應條件(或者日誌緩衝區寫滿)時,再將日誌寫到文件(或者同步到磁盤)中。能夠經過innodb_log_buffer_size參數設置其可使用的最大內存空間。默認是8MB,通常爲16~64MB便可。 innodb_flush_log_at_trx_commit = 1 #這個選項決定着何時把日誌信息寫入日誌文件以及何時把這些文件物理地寫(術語稱爲」同步」)到硬盤上。設置值0的意思是每隔一秒寫一第二天 志並進行 同步,這能夠減小硬盤寫操做次數,但可能形成數據丟失; 設置值1(設置設置)的意思是在每執行完一條COMMIT命令就寫一第二天志並進行同步,這能夠防止數據丟失,但硬盤寫操做可能會很頻繁; 設置值2是通常折衷的辦法,即每執行完一條COMMIT命令寫一第二天志,每隔一秒進行一次同步。 innodb_lock_wait_timeout = 50 #若是某個事務在等待n秒(s)後尚未得到所須要的資源,就使用ROLLBACK命令放棄這個事務。這項設置對於發現和處理未能被InnoDB數據表驅動 程序識別出來的死鎖條件有着重要的意義。這個選項的默認設置是50s。 # [mysqldump] # quick # max_allowed_packet = 16M # # [mysql] # no-auto-rehash # # [myisamchk] # key_buffer_size = 64M # sort_buffer_size = 1M # read_buffer = 2M # write_buffer = 2M # # [mysqlhotcopy] # interactive-timeout #
[client]
port =3306 socket =/tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock user = mysql
server_id = 10
datadir = /data/mysql/
old_passwords = 1
lower_case_table_names = 1
character-set-server = utf8
default-storage-engine = MYISAM
log-bin = bin.log
log-error = error.log
pid-file = mysql.pid
long_query_time = 2
slow_query_time = 2
slow_query_log
slow_query_log_file = slow.log
binlog_cache_size = 4MB
binlog_format = mixed
max_binlog_cache_size = 16MB
max_binlog_size = 1GB
expire_logs_days = 30
ft_min_word_len = 4
back_log = 512
max_allowed_packet = 64MB
max_connections = 4096
max_connect_errors = 100
join_buffer_size = 2MB
read_buffer_size = 2MB
read_rnd_buffer_size = 2MB
sort_buffer_size = 2MB
query_cache_size = 2MB
table_open_cache = 10000
thread_cache_size = 256
max_heap_table_size = 64MB
tmp_table_size = 64MB
thread_stack = 192KB
thread_concurrency = 24
local-infile = 0
skip-show-database
skip-name-resolve
skip-external-locking
connect_timeout = 600
interactive_timeout = 600
wait_timeout = 600
#MyISAM
key_buffer_size = 512MB
bulk_insert_buffer_size = 64MB
mysiam_sort_buffer_size = 64MB
mysiam_max_sort_file_size = 1GB
mysiam_repair_threads = 1
concurrent_insert = 2
myisam_recover
#INNODB
innodb_buffer_pool_size = 64G
innodb_additional_mem_pool_size = 32MB
innodb_data_file_path = ibdata1:1G;ibdata2:1G:autoextend
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_file_per_table = 1
innodb_flush_log_at_thx_commit = 2
innodb_lock_wait_timeout = 120
innodb_log_buffer_size = 8MB
innodb_log_file_size = 256MB
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_thread_concurrency = 16
innodb_open_files = 10000
#innodb_force_recovery = 4
#replication slave
read-only
#skip-salve-start
relay-log = relay.log
log-slave-updates
本文參考:
《曝光:Linux企業運維實戰》