Mysql Shell部署
cd /usr/local/
tar -xvf /root/mysql-shell-8.0.35-linux-glibc2.17-x86-64bit.tar.gz
chown -R mysql.mysql mysqlsh mysql-shell-8.0.35-linux-glibc2.17-x86-64bit
mysqlsh登录退出
mysqlsh -uroot -S /data/3306/mysql.sock
MySQL Shell 8.0.35
Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
Creating a session to 'root@/data%2F3306%2Fmysql.sock'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 132
Server version: 8.0.35 MySQL Community Server - GPL
No default schema selected; type \use to set one.
MySQL localhost JS > \q
Bye!
3种功能测试
\py
\sql
\js
集群管理
单主多主切换
MySQL localhost JS > dba.getCluster().switchToMultiPrimaryMode()
ERROR: Unable to connect to instance 'mgr801:3306'. Please, verify connection credentials and make sure the instance is available.
Cluster.switchToMultiPrimaryMode: Access denied for user 'root'@'192.168.56.41' (using password: YES) (RuntimeError)
报错处理,创建新的用户名密码
mysql> create user root@'192.168.56.%' identified by 'root' ;
Query OK, 0 rows affected, 2 warnings (0.11 sec)
mysql> grant all on . to root@'192.168.56.%' with grant option;
Query OK, 0 rows affected (0.19 sec)
成功切换
MySQL localhost JS > dba.getCluster().switchToMultiPrimaryMode()
Switching cluster 'demo1Cluster' to Multi-Primary mode...
Instance 'mgr801:3306' remains PRIMARY.
Instance 'mgr803:3306' was switched from SECONDARY to PRIMARY.
Instance 'mgr802:3306' was switched from SECONDARY to PRIMARY.
The cluster successfully switched to Multi-Primary mode.
MySQL localhost JS > dba.getCluster().switchToSinglePrimaryMode()
Switching cluster 'demo1Cluster' to Single-Primary mode...
Instance 'mgr801:3306' remains PRIMARY.
Instance 'mgr803:3306' was switched from PRIMARY to SECONDARY.
Instance 'mgr802:3306' was switched from PRIMARY to SECONDARY.
WARNING: Existing connections that expected a R/W connection must be disconnected, i.e. instances that became SECONDARY.
The cluster successfully switched to Single-Primary mode.
查看集群状态
MySQL localhost JS > dba.getCluster().status()
{
"clusterName": "demo1Cluster",
"defaultReplicaSet": {
"name": "default",
"primary": "mgr801:3306",
"ssl": "DISABLED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"mgr801:3306": {
"address": "mgr801:3306",
"memberRole": "PRIMARY",
"memberState": "(MISSING)",
"mode": "n/a",
"readReplicas": {},
"role": "HA",
"shellConnectError": "MySQL Error 1045: Could not open connection to 'mgr801:3306': Access denied for user 'root'@'192.168.56.41' (using password: YES)",
"status": "ONLINE",
"version": "8.0.35"
},
"mgr802:3306": {
"address": "mgr802:3306",
"memberRole": "SECONDARY",
"memberState": "(MISSING)",
"mode": "n/a",
"readReplicas": {},
"role": "HA",
"shellConnectError": "MySQL Error 1045: Could not open connection to 'mgr802:3306': Access denied for user 'root'@'192.168.56.41' (using password: YES)",
"status": "ONLINE",
"version": "8.0.35"
},
"mgr803:3306": {
"address": "mgr803:3306",
"memberRole": "SECONDARY",
"memberState": "(MISSING)",
"mode": "n/a",
"readReplicas": {},
"role": "HA",
"shellConnectError": "MySQL Error 1045: Could not open connection to 'mgr803:3306': Access denied for user 'root'@'192.168.56.41' (using password: YES)",
"status": "ONLINE",
"version": "8.0.35"
}
},
"topologyMode": "Single-Primary"
},
"groupInformationSourceMember": "mgr801:3306"
}
迁移数据
导出
MySQL localhost JS > util.dumpInstance('/home/mysql/test/')
Acquiring global read lock
Global read lock acquired
Initializing - done
1 out of 5 schemas will be dumped and within them 9 tables, 12 views, 10 routines.
6 out of 9 users will be dumped.
Gathering information - done
All transactions have been started
Locking instance for backup
Global read lock has been released
Writing global DDL files
Writing users DDL
Running data dump using 4 threads.
NOTE: Progress information uses estimated values and may not be accurate.
NOTE: Table statistics not available for mysqlinnodbclustermetadata.clusters, chunking operation may be not optimal. Please consider running 'ANALYZE TABLE mysqlinnodbclustermetadata.clusters;' first.
Writing schema metadata - done
Writing DDL - done
Writing table metadata - done
Starting data dump
200% (4 rows / ~2 rows), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed
Dump duration: 00:00:00s
Total duration: 00:00:00s
Schemas dumped: 1
Tables dumped: 9
Uncompressed data size: 1.30 KB
Compressed data size: 657 bytes
Compression ratio: 2.0
Rows written: 4
Bytes written: 657 bytes
Average uncompressed throughput: 1.30 KB/s
Average compressed throughput: 657.00 B/s
导入
数据导入需要设置 local_file参数
mysql> set global local_infile=on;
Query OK, 0 rows affected (0.00 sec)
MySQL localhost JS > util.loadDump("/home/mysql/test")
Loading DDL and Data from '/home/mysql/test' using 4 threads.
Opening dump...
Target is MySQL 8.0.35. Dump was produced from MySQL 8.0.35
Scanning metadata - done
Checking for pre-existing objects...
ERROR: Schema mysqlinnodbclustermetadata already contains a table named asynccluster_members
ERROR: Schema mysqlinnodbclustermetadata already contains a table named asynccluster_views
ERROR: Schema mysqlinnodbcluster_metadata already contains a table named clusters
ERROR: Schema mysqlinnodbclustermetadata already contains a table named clustersetmembers
ERROR: Schema mysqlinnodbclustermetadata already contains a table named clustersetviews
ERROR: Schema mysqlinnodbcluster_metadata already contains a table named clustersets
ERROR: Schema mysqlinnodbcluster_metadata already contains a table named instances
ERROR: Schema mysqlinnodbclustermetadata already contains a table named routerrest_accounts
ERROR: Schema mysqlinnodbcluster_metadata already contains a table named routers
ERROR: Schema mysqlinnodbclustermetadata already contains a view named schemaversion
ERROR: Schema mysqlinnodbclustermetadata already contains a view named v2ar_clusters
ERROR: Schema mysqlinnodbclustermetadata already contains a view named v2ar_members
ERROR: Schema mysqlinnodbclustermetadata already contains a view named v2clusters
ERROR: Schema mysqlinnodbclustermetadata already contains a view named v2cs_clustersets
ERROR: Schema mysqlinnodbclustermetadata already contains a view named v2cs_members
ERROR: Schema mysqlinnodbclustermetadata already contains a view named v2csrouteroptions
ERROR: Schema mysqlinnodbclustermetadata already contains a view named v2gr_clusters
ERROR: Schema mysqlinnodbclustermetadata already contains a view named v2instances
ERROR: Schema mysqlinnodbclustermetadata already contains a view named v2routerrestaccounts
ERROR: Schema mysqlinnodbclustermetadata already contains a view named v2routers
ERROR: Schema mysqlinnodbclustermetadata already contains a view named v2this_instance
ERROR: Schema mysqlinnodbclustermetadata already contains a procedure named v2csaddinvalidated_member
ERROR: Schema mysqlinnodbclustermetadata already contains a procedure named v2cs_created
ERROR: Schema mysqlinnodbclustermetadata already contains a procedure named v2csmemberadded
ERROR: Schema mysqlinnodbclustermetadata already contains a procedure named v2csmemberrejoined
ERROR: Schema mysqlinnodbclustermetadata already contains a procedure named v2csmemberremoved
ERROR: Schema mysqlinnodbclustermetadata already contains a procedure named v2csprimarychanged
ERROR: Schema mysqlinnodbclustermetadata already contains a procedure named v2csprimaryforce_changed
ERROR: Schema mysqlinnodbclustermetadata already contains a procedure named v2setglobalrouter_option
ERROR: Schema mysqlinnodbclustermetadata already contains a procedure named v2setroutingoption
ERROR: Schema mysqlinnodbclustermetadata already contains a procedure named v2begincs_change
ERROR: One or more objects in the dump already exist in the destination database. You must either DROP these objects or exclude them from the load.
Util.loadDump: While 'Scanning metadata': Duplicate objects found in destination database (MYSQLSH 53021)