mysql日常操作

Posted 子鱼非安

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql日常操作相关的知识,希望对你有一定的参考价值。

一、二进制安装

二、主从复制

三、sql语句

二进制安装mysql

rpm -e mariadb-libs --nodepscp /root/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz /usr/local/cd /usr/localtar -zxf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gzmv mysql-5.7.30-linux-glibc2.12-x86_64 /usr/local/mysqlcd /etcmv my.cnf my.cnf.bakgroupadd mysqluseradd -g mysql mysql

cat >> /etc/my.cnf << eof[client]#character-set-server=utf8port=3306socket=/usr/local/mysql/data/mysql.sock[mysqld]#skip-grant-tablesport=3306socket=/usr/local/mysql/data/mysql.sockbasedir=/usr/local/mysqldatadir=/usr/local/mysql/datamax_connections=200log-error=/usr/local/mysql/mysql-error.logcharacter-set-server=utf8default-storage-engine=innodbuser=mysql[mysqld_safe]pid-file=/usr/local/mysql/data/n1.pideof

yum install -y libaio-devel.x86_64
chown -r mysql:mysql /var/lib.mysqlmkdir /var/lib/mysql
mkdir /usr/local/mysql/datachown -r mysql:mysql /usr/local/mysqlchown -r mysql:mysql datachown 777 /etc/my.cnf./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqldchmod +x /etc/init.d/mysqldsystemctl daemon-reloadecho 'mysql_home=/usr/local/mysql' >> /etc/profileecho 'path=$path:$mysql_home/bin' >> /etc/profileecho 'export path' >> /etc/porfilesource /etc/profilesystemctl start mysqldpassword=`grep 'generated' /usr/local/mysql//mysql-error.log | awk -f ' ' {print'$11'}`mysqladmin -u root -p$password password 123mysql -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 -pmysql> 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账户mysql> 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 = 1log-bin=mysql-bin 添加完后重启mysql1.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 = 2relay-log = /usr/local/mysql/data/relay-log-binrelay-log-index = /usr/local/mysql/data/slave-relay-bin.indexssl-ca = /usr/local/mysql/data/ca.pemssl-cert = /usr/local/mysql/data/client-cert.pemssl-key = /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的ip2.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: 0Master_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)主要查看下面两行是否yesSlave_IO_Running: YesSlave_SQL_Running: Yes2.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_schemaselect 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 会读取右边数据表的全部数据,即便左边表无对应数据。注意onwhere有什么区别,两个表连接时用on,在使用left jion时,onwhere条件的区别如下:1on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。2where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有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就是排序。而groupby就是分组,举个例子好说点,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日常操作的主要内容,如果未能解决你的问题,请参考以下文章

MySQL 日常操作记录

Mysql日常操作

MySQL日常操作

mysql日常操作

VsCode 代码片段-提升研发效率

sublimetext3中保存代码片段