数据库系列-MySQL安装部署篇

一 单节点安装
1 查询mariadb的版本号
rpm -qa|grep mariadb  #如显示出:mariadb-libs-5.5.56-2.el7.x86_64,则说明系统使用的是该版本,可以对该版本进行卸载
sudo rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps         #卸载mariadb
2 安装
 2.1 下载地址
https://downloads.mysql.com/archives/community/
 2.2 安装
tar xvf  mysql-5.7.19-1.el6.x86_64.rpm-bundle.tar.    #解压
建议按顺序安装:common --> libs --> clients --> server	
rpm -ivh mysql-community-common-5.7.19-1.el7.x86_64.rpm --nodeps
rpm -ivh mysql-community-libs-5.7.19-1.el7.x86_64.rpm   --nodeps
rpm -ivh mysql-community-libs-compat-5.7.19-1.el7.x86_64.rpm  --nodeps
rpm -ivh mysql-community-client-5.7.19-1.el7.x86_64.rpm  --nodeps
rpm -ivh mysql-community-server-5.7.19-1.el7.x86_64.rpm  --nodeps
rpm -ivh mysql-community-devel-5.7.19-1.el7.x86_64.rpm  --nodeps
3 启动
systemctl start mysql
二 集群信息
1 重要配置
重要配置:config.ini(家目录)、my.cnf(/etc/)
note:/etc/my.cnf   chmod 644 my.cnf  #权限要改成可读即可,要不启动不起来
2 停集群步骤
 2.1 停管理节点
cd /laity/home/mysql/mysql/bin
./ndb_mgm -e shutdown           #如果有多个管理节点,管理节点杀掉1个即可
 2.2 停sql节点
cd /laity/home/mysql/mysql/bin
./mysqladmin -uroot -h 127.0.0.1 -proot123 shutdown  #使用root密码,这里的root指的是mysql的root,不是服务器上的root用户
3 启动集群步骤
 3.1 启动管理节点
cd /laity/home/mysql/mysql/bin
./ndb_mgmd -f /laity/home/mysql/config.ini --configdir=/laity/home/mysql/mysql-cluster  --initial    #初始化管理节点配置
./ndb_mgmd -f /laity/home/mysql/config.ini --configdir=/laity/home/mysql/mysql/mysql-cluster  --reload     #重新加载管理节点配置
 3.2 检查进程
ps -ef |grep ndb_mgmd
mysql    146287 146253  0 11:27 pts/0    00:00:00 grep --color=auto ndb_mgmd
mysql    539141      1  0 9月06 ?       00:22:13 ./ndb_mgmd -f /laity/home/mysql/config.ini --configdir=/laity/home/mysql/cluster --initial
 3.3 启动数据节点
cd /laity/home/mysql/mysql/bin
./ndbd
 3.4 检查进程
ps -ef |grep ndbd
mysql     36467      1  0 9月06 ?       00:00:40 ./ndbd
mysql     36468  36467 18 9月06 ?       15:21:55 ./ndbd
 3.5 启动sql节点
cd /laity/home/mysql/mysql/bin
nohup ./mysqld_safe>/dev/null  &
 3.6 检查进程
ps -ef |grep safe
mysql    289588 289568  0 11:29 pts/0    00:00:00 grep --color=auto safe
mysql    401738      1  0 9月06 ?       00:00:00 /bin/sh ./mysqld_safe
4 查询节点状态是否正常和内存使用率
 4.1 查看节点状态   	
cd /laity/home/mysql/mysql/bin
./ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: xxx.xxx.xxx.xxx:port
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)    # 数据节点
id=3    @xxx.xxx.xxx.xxx  (mysql-5.7.19 ndb-7.5.7, Nodegroup: 0, *)
id=4    @xxx.xxx.xxx.xxx  (mysql-5.7.19 ndb-7.5.7, Nodegroup: 0)

[ndb_mgmd(MGM)] 2 node(s)    #管理节点
id=222  @xxx.xxx.xxx.xxx  (mysql-5.7.19 ndb-7.5.7)
id=223  @xxx.xxx.xxx.xxx  (mysql-5.7.19 ndb-7.5.7)

[mysqld(API)]   7 node(s)     #sql节点
id=100  @xxx.xxx.xxx.xxx  (mysql-5.7.19 ndb-7.5.7)
id=101  @xxx.xxx.xxx.xxx  (mysql-5.7.19 ndb-7.5.7)
id=102  @xxx.xxx.xxx.xxx  (mysql-5.7.19 ndb-7.5.7)
id=103  @xxx.xxx.xxx.xxx  (mysql-5.7.19 ndb-7.5.7)
id=104  @xxx.xxx.xxx.xxx  (mysql-5.7.19 ndb-7.5.7)
id=105  @xxx.xxx.xxx.xxx  (mysql-5.7.19 ndb-7.5.7)
id=106  @xxx.xxx.xxx.xxx  (mysql-5.7.19 ndb-7.5.7)

 4.2 内存使用率
cd /laity/home/mysql/mysql/bin
./ndb_mgm
ndb_mgm> ALL REPORT MEMORYUSAGE
Node 3: Data usage is 82%(433792 32K pages of total 524288)
Node 3: Index usage is 26%(138770 8K pages of total 524320)
Node 4: Data usage is 82%(433792 32K pages of total 524288)
Node 4: Index usage is 26%(138770 8K pages of total 524320)
三 mysql基本
1 用户名、密码

1.1 更改用户名密码

[mysql@mobile-2021-4fecbe07 bin]$ pwd
/export/home/mysql/mysql/bin
[mysql@mobile-2021-4fecbe07 bin]$ ./mysql -uroot -proot123456
mysql> update mysql.user set authentication_string=password('zabbix456') where user='zabbix';  #将zabbix用户密码修改为zabbix456  
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1
note:MySQL5.7的user表中的密码字段从Password变成了authentication_string,如果你使用的是MySQL5.7之前的版本,将authentication_string字段替换成Password即可

mysql> flush privileges;  #刷新权限,需用flush privileges刷新MySQL的系统权限相关表
Query OK, 0 rows affected (0.00 sec)

1.2 创建用户

方法一:
mysql>create user 'zabbix'@% identified by 'zabbix123';  #% 允许客户端指定用户从任意主机连接
方法二:
mysql> GRANT SELECT ON *.* TO 'test3'@localhost IDENTIFIED BY 'test3';
# select:给用户赋予的权限; *.*:在指定的数据库和表上使用自己的权限(*.*就是database.table);
GRANT语句是MySQL中一个非常重要的语句,它可以用来创建用户、修改用户密码和设置用户权限。

1.3 删除用户

方法一:
drop user [用户1],[用户2]
mysql> DROP USER 'test1'@'localhost';
Query OK, 0 rows affected (0.00 sec)
--使用 DROP USER 语句应注意以下几点:
--DROP USER 语句可用于删除一个或多个用户,并撤销其权限。
--使用 DROP USER 语句必须拥有 mysql 数据库的 DELETE 权限或全局 CREATE USER 权限。
--在 DROP USER 语句的使用中,若没有明确地给出账户的主机名,则该主机名默认为“%”。
note:用户的删除不会影响他们之前所创建的表、索引或其他数据库对象,因为 MySQL 并不会记录是谁创建了这些对象。
方法二:
mysql>delete from mysql.user where Host='localhost' and User='test1';
mysql>flush privileges;  #使用delete删除用户,必须进行刷新,否则重新创建该用户会报错;
--报错信息
--ERROR 1396 (HY000): Operation CREATE USER failed for ‘abc’@’localhost’

1.4 查看用户权限

show grants for 'cbilling';
结果:GRANT USAGE ON *.* TO 'zabbix'@'%'   #USAGE ON *.* 用户没有任何权限

1.5 给用户授权

grant select,insert on *.* to 'zabbix'@'%' with grant option;

1.6 删除用户权限

revoke select,insert on *.* from 'zabbix'@'%';
2 mysql登录方式
mysql -h hostname|hostlP -p port -u username -p DatabaseName -e "SQL语句"
--参数详解
-h:指定连接 MySQL 服务器的地址。可以用两种方式表示,hostname 为主机名,hostIP 为主机 IP 地址。
-p:指定连接 MySQL 服务器的端口号,port 为连接的端口号。MySQL 的默认端口号是 3306,因此如果不指定该参数,默认使用 3306 连接 MySQL 服务器。
-u:指定连接 MySQL 服务器的用户名,username 为用户名。
-p:提示输入密码,即提示 Enter password。
DatabaseName:指定连接到 MySQL 服务器后,登录到哪一个数据库中。如果没有指定,默认为 mysql 数据库。
-e:指定需要执行的 SQL 语句,登录 MySQL 服务器后执行这个 SQL 语句,然后退出 MySQL 服务器。

/note:参数-p后面是否有空格意义完全不一样/

[mysql@jdmobile-2021-4fecbe07 bin]$ pwd
/export/home/mysql/mysql/bin 
登录方式1:
[mysql@mobile bin]$ ./mysql -h localhost -u root -p zabbix   #zabbix 指的是zabbix数据库
Enter password: *****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1945
Server version: 5.7.19-ndb-7.5.7-cluster-gpl-log MySQL Cluster Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.	
mysql>

登录方式2:
[mysql@mobile bin]$ ./mysql -h localhost -uroot -p123456   #123456 指的是密码
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1946
Server version: 5.7.19-ndb-7.5.7-cluster-gpl-log MySQL Cluster Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
posted @ 2022-06-14 22:46  laity_guan  阅读(42)  评论(0编辑  收藏  举报