mysql日常操作
Posted 子鱼非安
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql日常操作相关的知识,希望对你有一定的参考价值。
一、二进制安装
二、主从复制
三、sql语句
二进制安装mysql
rpm -e mariadb-libs --nodeps
cp /root/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz /usr/local/
cd /usr/local
tar -zxf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.30-linux-glibc2.12-x86_64 /usr/local/mysql
cd /etc
mv my.cnf my.cnf.bak
groupadd mysql
useradd -g mysql mysql
cat >> /etc/my.cnf << eof
[client]
#character-set-server=utf8
port=3306
socket=/usr/local/mysql/data/mysql.sock
[mysqld]
#skip-grant-tables
port=3306
socket=/usr/local/mysql/data/mysql.sock
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
max_connections=200
/usr/local/mysql/mysql-error.log =
utf8 =
innodb =
user=mysql
[mysqld_safe]
/usr/local/mysql/data/n1.pid =
eof
yum install -y libaio-devel.x86_64
chown -r mysql:mysql /var/lib.mysql
mkdir /var/lib/mysql
mkdir /usr/local/mysql/data
chown -r mysql:mysql /usr/local/mysql
chown -r mysql:mysql data
chown 777 /etc/my.cnf
--initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
systemctl daemon-reload
echo 'mysql_home=/usr/local/mysql' >> /etc/profile
echo 'path=$path:$mysql_home/bin' >> /etc/profile
echo 'export path' >> /etc/porfile
source /etc/profile
systemctl start mysqld
password=`grep 'generated' /usr/local/mysql//mysql-error.log | awk -f ' ' {print'$11'}`
mysqladmin -u root -p$password password 123
mysql -u root -p123
主从复制
一、 在主mysql上操作
1.1.创建ssl文件
mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
chmod +r /usr/local/mysql/data/server-key.pem 警告不执行此命令重启容易报错
1.2.重启mysql然后登录查看是否支持ssl
systemctl restart mysql
mysql -uroot -p
show variables like '%ssl%';
+---------------+-----------------+
Variable_name | Value |
+---------------+-----------------+
have_openssl | YES |
have_ssl | YES |
ssl_ca | ca.pem |
ssl_capath | |
ssl_cert | server-cert.pem |
ssl_cipher | |
ssl_crl | |
ssl_crlpath | |
ssl_key | server-key.pem |
+---------------+-----------------+
9 rows in set (0.00 sec)
1.3.创建require ssl账户
grant replication slave on *.* to 'ssl'@'192.168.1.%' identified by 'pass@123' require ssl;
Query OK, 0 rows affected, 1 warning (0.00 sec)
这里的192.168.1.%是指从mysql的ip网段
1.4.在/etc/my.cnf添加以下内容
server_id = 1
mysql-bin 添加完后重启mysql =
1.5.把主mysql上生成的证书scp给从mysql
scp ca.pem client-cert.pem client-key.pem root@192.168.1.6:/usr/local/mysql/data/
这里输入的ip地址是从mysql的ip
二、 在从mysql上操作
2.1.在/etc/my.cnf添加以下内容
server_id = 2
/usr/local/mysql/data/relay-log-bin =
/usr/local/mysql/data/slave-relay-bin.index =
/usr/local/mysql/data/ca.pem =
/usr/local/mysql/data/client-cert.pem =
/usr/local/mysql/data/client-key.pem =
# server_id必须唯一,不可以和其他mysql上id一样。
2.2.添加写权限,然后重启mysql,查看是否支持ssl
chmod +r client-key.pem
systemctl restart mysql
查看是支持请查看2.2步骤
2.3.在主mysql查看mysq
mysql master status;
2.4.在从mysql在配置change master to
change master to master_host='192.168.1.6',master_user='rep',master_password='pass@123',master_log_file='mysql-bin.000001',master_log_pos=154,master_ssl=1,master_ssl_ca='/usr/local/mysql/data/ca.pem',master_ssl_cert='/usr/local/mysql/data/client-cert.pem',master_ssl_key='/usr/local/mysql/data/client-key.pem';
master_host:是指主mysql的ip
2.5.在从mysql启动从,然后查看状态
show slave status\G
1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: relay-log-bin.000009
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 738
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: Yes
Master_SSL_CA_File: /usr/local/mysql/data/ca.pem
Master_SSL_CA_Path:
Master_SSL_Cert: /usr/local/mysql/data/client-cert.pem
Master_SSL_Cipher:
Master_SSL_Key: /usr/local/mysql/data/client-key.pem
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 58f5343c-2758-11eb-8f2e-000c290194aa
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
主要查看下面两行是否yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
2.6显示yes说明ssl主从搭建完成,也可以在主上create数据,在从上select测试
SQL语句
创建批量插入一万条数据
drop procedure if exists top;
delimiter //
create procedure top() begin declare i int; set i=1; while(i<=1000)do insert into top value (i,repeat('wang', 2-1)); set i=i+1; end while; end//
delimiter ;
call top();
授权root远程登录
use mysql;
select 'host' from user where user='root';
update user set host = '%' where user ='root';
flush privileges;
# 修改密码
alter user 'root'@'localhost' identified by 'pass@123';
创建用户
create user 'zabbix'@'%' identified by 'zabbix';
grant all on *.* to 'zabbix'@'%'; localhost或%都可以
select user host from user where host="localhost";
delete from user where user="username" and host="localhost";
查看数据库所占空间大小
use information_schema
select concat(round(sum(data_length/1024/1024),2),'mb') as data from tables where table_schema='test1' ;
查看表所占空间大小
select concat(round(sum(data_length/1024/1024),2),'mb') as data from tables where table_schema='test' and table_name='test1';
删除字段
mysql alter table table_name drop field_name;
获取表的行数
select
count(*)
from
table_name;
删除多行
delete from student where id>100
停止存储过程
show processlist;
kill 进程号
查看数据所占字节大小
mysql> select length('网吧滚');
+---------------------+
| length('网吧滚') |
+---------------------+
| 9 |
+---------------------+
1 row in set (0.00 sec)
查看表字段中的数据所占字节大小
mysql> select length(first_name) from employees;
多表查询 and为同时满足的条件
方法一:从小到大排序
select s.*,d.dept_no from salaries s ,dept_manager d where s.to_date='9999-01-01' and d.to_date='9999-01-01' and s.emp_no = d.emp_no order by from_date asc;
方法二:
select s.* ,d.dept_no from salaries as s join dept_manager as d on s.emp_no=d.emp_no where s.to_date = '9999-01-01' and d.to_date='9999-01-01';
左连接查询
select a.last_name,a.first_name,b.dept_no from employees a left join dept_emp b on a.emp_no = b.emp_no;
注意:
inner join 两边表同时有对应的数据,即任何一边缺失数据就不显示。
left join 会读取左边数据表的全部数据,即便右边表无对应数据。
right join 会读取右边数据表的全部数据,即便左边表无对应数据。
注意on与where有什么区别,两个表连接时用on,在使用left
jion时,on和where条件的区别如下:
1、
on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。
2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left
join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
分组查询
select emp_no,count(emp_no) as a from salaries group by emp_no having a > 15;
还有就是group by与order by有什么区别,order by就是排序。而group
by就是分组,举个例子好说点,group by 单位名称
这样的运行结果就是以“单位名称”为分类标志统计各单位的职工人数和工资总额。
这样可以更好的分下类,更好看一些。
还有就是为什么没有用where而是用的having,记住下面的两句话就好了
where语句在group by语句之前;sql会在分组之前计算where语句。
having语句在group by语句之后;sql会在分组之后计算having语句。
去重查询
select distinct salary from salaries order by salary desc;
select salary from salaries group by;
select salary from salaries group by salary order by salary desc;
对于distinct与group by的使用:
1、当对系统的性能高并数据量大时使用group by
2、当对系统的性能不高时使用数据量少时两者皆可
3、尽量使用group by
以上是关于mysql日常操作的主要内容,如果未能解决你的问题,请参考以下文章