mysql 常用
基本信息
創建庫和授權
mysql> create database mycat_eye; Query OK, 1 row affected (0.01 sec) --創建mycat_eye庫 mysql> GRANT all ON mycat_eye.* TO root@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.03 sec)--給dss用戶對mycat_eye庫授權 mysql> flush privileges; Query OK, 0 rows affected (0.02 sec)--刷新系統權限表 顯示表記錄 mysql> select table_name,concat(round((DATA_LENGTH+INDEX_LENGTH)/1024/1024,2),'MB') as size,table_rows from information_schema.tables where table_schema="mycat_ eye" order by table_rows desc limit 10; +----------------------+---------+------------+ | table_name | size | table_rows | +----------------------+---------+------------+ | mysql_status_history | 18.06MB | 45362 | | mysql_server | 0.03MB | 3 | | mysql_cluster | 0.02MB | 1 | | sys_user | 0.05MB | 1 | | mysql_weak_password | 0.03MB | 1 | +----------------------+---------+------------+ 5 rows in set (0.00 sec)二進制
mysql> SHOW BINARY LOGS; +-----------+-----------+ | Log_name | File_size | +-----------+-----------+ | ON.000001 | 87199642 | +-----------+-----------+ 1 row in set (0.00 sec)mysql> SHOW BINLOG EVENTS IN 'ON.000001' LIMIT 10, 8; +-----------+------+----------------+-----------+-------------+--------------------------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +-----------+------+----------------+-----------+-------------+--------------------------------------------------------------------------------+ | ON.000001 | 1286 | Anonymous_Gtid | 1122 | 1351 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | ON.000001 | 1351 | Query | 1122 | 1428 | BEGIN | | ON.000001 | 1428 | Table_map | 1122 | 1494 | table_id: 108 (mycat_eye.mysql_cluster) | | ON.000001 | 1494 | Write_rows | 1122 | 1556 | table_id: 108 flags: STMT_END_F | | ON.000001 | 1556 | Xid | 1122 | 1587 | COMMIT /* xid=45 */ | | ON.000001 | 1587 | Anonymous_Gtid | 1122 | 1652 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | ON.000001 | 1652 | Query | 1122 | 1797 | use `mycat_eye`; DROP TABLE IF EXISTS `mysql_server` /* generated by server */ | | ON.000001 | 1797 | Anonymous_Gtid | 1122 | 1862 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | +-----------+------+----------------+-----------+-------------+--------------------------------------------------------------------------------+ 8 rows in set (0.00 sec)主Master信息 mysql> show master status; +-----------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-----------+----------+--------------+------------------+-------------------+ | ON.000001 | 87581468 | | | | +-----------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)mysql> show slave hosts; +-----------+------+------+-----------+--------------------------------------+ | Server_id | Host | Port | Master_id | Slave_UUID | +-----------+------+------+-----------+--------------------------------------+ | 1127 | | 3307 | 1122 | ec83bdff-d4d1-11e7-a203-00ff3dca5ad5 | +-----------+------+------+-----------+--------------------------------------+ 1 row in set (0.00 sec)
從slave信息 mysql> show slave status\G; *************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 127.0.0.1Master_User: rootMaster_Port: 3306Connect_Retry: 60Master_Log_File: ON.000001Read_Master_Log_Pos: 87963280Relay_Log_File: AHQ-PC-01057-relay-bin.000005Relay_Log_Pos: 6680878Relay_Master_Log_File: ON.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 87963280Relay_Log_Space: 87963503Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 1122Master_UUID: ec83bdff-d4d1-11e7-a203-00ff3dca5ad4Master_Info_File: D:\Mysql\MySQL5.72\data\master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 0Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version: 1 row in set (0.00 sec)
主從配置
1、主從服務器分別作以下操作:
? ?版本一致
2、修改主服務器master:
? ?linux ?my.cnf
? ?win ?my-default.ini
? ? [mysqld]
? ? log_bin ? ? ? ? = ON ? //[必須]啟用二進制日志
? ? binlog_format ? = ROW
? ? server_id = 1122 //[必須]服務器唯一ID
3、修改從服務器slave:
? ?#vi /etc/my.cnf
? ?[mysqld]
? ?log_bin ?= ON ? //[不是必須]啟用二進制日志
? ?server_id = 1123 //[必須]服務器唯一ID
? ?執行?
? ?stop slave;
? ?change master to master_host='127.0.0.1',master_user='root',master_password='123456',master_log_file='ON.000001',master_log_pos=463;
? ?start slave;
解決 Slave_SQL_Running: No
stop slave;
set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;
#免密碼登錄
my-default.ini的[mysqld]新增
#免密碼登錄
skip-grant-tables
#修改管理員密碼
#update user set password=PASSWORD("123456") where user='root';
半同步
? 在半同步復制中, 為了保證主庫上的每一個binlog事務都能夠被可靠的復制到從庫上,主庫在每次事務成功提交時, 并不及時反饋給前端應用用戶,而是等待其中之一個從庫也接收到Binlog事務并成功寫入中繼日志后, 主庫才返回Commit操作成功給客戶端。如果同步出現故障, 則MySQL自動調整復制為異步模式,事務正常返回提交結果給客戶端。半同步服務需要安裝插件.
1. 首先判斷MySQL服務器是否支持動態增加插件
mysql> select @@have_dynamic_loading;
+------------------------+
| @@have_dynamic_loading |
+------------------------+
| YES ? ? ? ? ? ? ? ? ? ?|
+------------------------+
1 row in set (0.00 sec)
2 安裝插件
#查看插件存入地址
mysql> show variables like 'plugin_dir';
+---------------+------------------------------------------+
| Variable_name | Value ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
+---------------+------------------------------------------+
| plugin_dir ? ?| D:\mysql\mysql-5.6.25-winx64\lib\plugin\ |
+---------------+------------------------------------------+
1 row in set (0.00 sec)
#Master安裝 semisync_master.dll(linux安裝semisync_master.so)
mysql> install plugin rpl_semi_sync_master SONAME 'semisync_master.dll';
Query OK, 0 rows affected (0.07 sec)
#Slave安裝(linux安裝semisync_slave.so)
mysql> install plugin rpl_semi_sync_slave SONAME 'semisync_slave.dll';
Query OK, 0 rows affected (0.49 sec)
#查看Master
mysql> select * from mysql.plugin;
+----------------------+---------------------+
| name ? ? ? ? ? ? ? ? | dl ? ? ? ? ? ? ? ? ?|
+----------------------+---------------------+
| rpl_semi_sync_master | semisync_master.dll |
+----------------------+---------------------+
1 row in set (0.00 sec)
#查看Slave
mysql> select * from mysql.plugin;
+---------------------+--------------------+
| name ? ? ? ? ? ? ? ?| dl ? ? ? ? ? ? ? ? |
+---------------------+--------------------+
| rpl_semi_sync_slave | semisync_slave.dll |
+---------------------+--------------------+
1 row in set (0.10 sec)
3.分別配置主從數據庫my-default.ini, 打開半同步semi-sync
Master(linux my.cnf)
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=30000
Slave(my.cnf)
rpl_semi_sync_slave_enabled=1
4.檢查
Master
mysql> show status like '%semi_sync%';
+--------------------------------------------+-------+
| Variable_name ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients ? ? ? ? ? ? ? | 0 ? ? |
| Rpl_semi_sync_master_net_avg_wait_time ? ? | 0 ? ? |
| Rpl_semi_sync_master_net_wait_time ? ? ? ? | 0 ? ? |
| Rpl_semi_sync_master_net_waits ? ? ? ? ? ? | 0 ? ? |
| Rpl_semi_sync_master_no_times ? ? ? ? ? ? ?| 0 ? ? |
| Rpl_semi_sync_master_no_tx ? ? ? ? ? ? ? ? | 0 ? ? |
| Rpl_semi_sync_master_status ? ? ? ? ? ? ? ?| ON ? |
| Rpl_semi_sync_master_timefunc_failures ? ? | 0 ? ? |
| Rpl_semi_sync_master_tx_avg_wait_time ? ? ?| 0 ? ? |
| Rpl_semi_sync_master_tx_wait_time ? ? ? ? ?| 0 ? ? |
| Rpl_semi_sync_master_tx_waits ? ? ? ? ? ? ?| 0 ? ? |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 ? ? |
| Rpl_semi_sync_master_wait_sessions ? ? ? ? | 0 ? ? |
| Rpl_semi_sync_master_yes_tx ? ? ? ? ? ? ? ?| 0 ? ? |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
mysql> show variables like '%Rpl%';
+------------------------------------+----------+
| Variable_name ? ? ? ? ? ? ? ? ? ? ?| Value ? ?|
+------------------------------------+----------+
| rpl_semi_sync_master_enabled ? ? ? | ON ? ? ? |
| rpl_semi_sync_master_timeout ? ? ? | 30000 ? ?|
| rpl_semi_sync_master_trace_level ? | 32 ? ? ? |
| rpl_semi_sync_master_wait_no_slave | ON ? ? ? |
| rpl_stop_slave_timeout ? ? ? ? ? ? | 31536000 |
+------------------------------------+----------+
5 rows in set (0.00 sec)
Slave
mysql> show status like '%semi_sync%';
+----------------------------+-------+
| Variable_name ? ? ? ? ? ? ?| Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | OFF ? |
+----------------------------+-------+
1 row in set (0.02 sec)
mysql> show variables like '%Rpl%';
+---------------------------------+----------+
| Variable_name ? ? ? ? ? ? ? ? ? | Value ? ?|
+---------------------------------+----------+
| rpl_semi_sync_slave_enabled ? ? | ON ? ? ? |
| rpl_semi_sync_slave_trace_level | 32 ? ? ? |
| rpl_stop_slave_timeout ? ? ? ? ?| 31536000 |
+---------------------------------+----------+
3 rows in set (0.00 sec)
總結
- 上一篇: Mycat社区出版: 分布式数据库架构及
- 下一篇: mysql5.7主从