Ubuntu 安装 MySQL
安装
apt install mysql-server
卸载
apt remove mysql-server
1、sudo apt purge mysql-*
2、sudo rm -rf /etc/mysql/ /var/lib/mysql
3、sudo apt autoremove
4、sudo apt autoreclean
重要:Mysql 5.7.x及以上的版本 可能没有初始密码,
直接输入:mysql -u root 就直接进入mysql了
可以这样设置初始化密码(先mysql -u root进入mysql),其他很多方法无效:
alter user 'root'@'localhost' identified with mysql_native_password by '123456';
此时,远程客户端可以通过ssh,使用root:123456登录mysql,不需要开启3306端口
修改密码(在普通的命令行中操作,不用登录mysql)
mysqladmin -u root -p password 回车,此时系统要求输入旧密码、回车,然后输入新密码、回车
设置初始密码(此方法无效)
mysqladmin -u root password 123456
远程访问失败的问题(这样不安全)
1、安装mysql之后,修改配置文件:/etc/mysql/mysql.conf.d/mysqld.cnf,改为bind-address = 0.0.0.0
2、grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
3、flush privileges;
或者通过ssh访问,也就是不用开启mysql种root账户的远程权限
启动Mysql(以下三种任一)
systemctl start mysql
service mysqld start
/etc/rc.d/init.d/mysqld start
检查启动结果
netstat -tulnp | grep :3306
查看状态
systemctl status mysql
停止Mysql
systemctl stop mysql
登录Mysql
mysql -u root -p回车后输入密码
添加表字段
ALTER TABLE xxx ADD yyy INT DEFAULT 0;
删除表字段
ALTER TABLE xxx DROP yyy;
设置表字段默认值
ALTER TABLE xxx ALTER yyy SET DEFAULT 0;
查看数据库
show databases;
创建数据库
create database db_name;
选择数据库
use db_name;
查看当前库中的表
show tables;
选择当前库中的表
use table_name;
查看表中所有的数据
select * frome talbe_name;
查看表中所有的字段
show columns from talbe_name;
使用uid来查询id和card,判断是否存在该用户,
select id,card from user where uid=unionid;
不存在,则插入新用户
insert into user (uid) values (unionid);
存在,则返回id和房卡数据
select id,card where uid=unionid;
修改属性:
update table set card = card-1 where uid = 353500 and card > 0;
今天0点截止到当前时间的记录数
select count(*) from table where to_days( date ) = to_days( now() );
select count(*) from mjgamedata.t_roominfo where to_days( '时间字段' ) = to_days( now() );