零.安装
sudo apt-get install mysql-server mysql-client
//查看是状态
sudo service mysql status
一.字符集
//查
show variables like "%char%";
A、避免创建数据库及表出现中文乱码和查看编码方法
1、创建数据库的时候:CREATE DATABASE `test` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
2、建表的时候 CREATE TABLE `dax_user`(`ID` varchar(40) NOT NULL default '',`UserID` varchar(40) NOT NULL default '') ENGINE=InnoDB DEFAULT CHARSET=utf8;
B、更改
//设置默认编码为utf8:
set names utf8;
//等同于:
SET character_set_client='utf8';
SET character_set_connection='utf8';
SET character_set_results='utf8';
//设置数据库db_name默认为utf8:
ALTER DATABASE `db_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
//设置表tb_name默认编码为utf8:
ALTER TABLE `tb_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
编辑/etc/mysql/my.cnf文件,相当于windows中的my.ini:
找到[client] 添加:
default-character-set = utf8 // 默认字符集为utf8
找到[mysqld] 添加:
网上大部分是:
#default-character-set = utf8 //默认字符集为utf8
#init_connect = 'SET NAMES utf8' //设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行
其实是:
character_set_server=utf8
可能是版本差异,我的环境是(select version();) :
5.5.40-0ubuntu0.14.04.1
修改好后,重新启动mysql 即可. /etc/init.d/mysql restart
1、启动
MySQL安装完成后启动文件mysql在/etc/init.d目录下,
在需要启动时运行下面命令即可。
[root@test1 init.d]# /etc/init.d/mysql start service mysql start 2、停止 /usr/bin/mysqladmin -u root -p shutdown
3、自动启动
1)察看mysql是否在自动启动列表中
[root@test1 local]# /sbin/chkconfig –list
2)把MySQL添加到你系统的启动服务组里面去
[root@test1 local]# /sbin/chkconfig – add mysql
3)把MySQL从启动服务组里面删除。
[root@test1 local]# /sbin/chkconfig – del mysql
//Start mysql error:
ps -A|grep
sudo kill -9 xxxx
/etc/init.d/mysql start OK
more /var/log/syslog
三、日常管理
//导出sql文件
mysqldump -uroot -p testdb > testdb.sql
//导入
mysqljump -uroot -p testdb < testdb.sql
or
进入mysql后:
mysql> source testdb.sql;
//drop db
drop database testdb;
//create db
//增加用户
grant select,insert,update,delete on testdb.* to username@localhost Identified by "pwd";
or
CREATE USER demo IDENTIFIED BY “123456” ;
//授權
grant ALL PRIVILEGES ON tesdb.* to app@"%" identified by "app" WITH GRANT OPTION; or
grant all privileges on *.* to joe@you.ip identified by '123′;
//更改用户密码 mysqladmin -u USER -p password pwd //or login with root 1. UPDATE user SET password=PASSWORD('123456') WHERE user='root'; 2.SET PASSWORD FOR root=PASSWORD('123456'); 更改生效
FLUSH PRIVILEGES;
修改的用户都以root为列。
一、拥有原来的myql的root的密码;
方法一:
在mysql系统外,使用mysqladmin
# mysqladmin -u root -p password "test123"
Enter password: 【输入原来的密码】
方法二:
通过登录mysql系统,
# mysql -uroot -p
Enter password: 【输入原来的密码】
mysql>use mysql;
mysql> update user set password=passworD("test") where user='root';
mysql> flush privileges;
mysql> exit;
二、忘记原来的myql的root的密码;
首先,你必须要有操作系统的root权限了。要是连系统的root权限都没有的话,先考虑root系统再走下面的步骤。
类似于安全模式登录系统,有人建议说是pkill mysql,但是我不建议哈。因为当你执行了这个命令后,会导致这样的状况:
/etc/init.d/mysqld status
mysqld dead but subsys locked
这样即使你是在安全模式下启动mysql都未必会有用的,所以一般是这样/etc/init.d/mysqld stop,如果你不幸先用了pkill,那么就start一下再stop咯。
# mysqld_safe --skip-grant-tables &
&,表示在后台运行,不再后台运行的话,就再打开一个终端咯。
# mysql
mysql> use mysql;
mysql> UPDATE user SET password=password("test123") WHERE user='root';
mysql> flush privileges;
mysql> exit;
##本来mysql是不分大小写的,但是这个是修改的mysql中的mysql数据库的具体的值,要注意到。
四、处理Comment
1 创建表的时候写注释
create table test1
(
field_name int comment '字段的注释'
)comment='表的注释';
2 修改表的注释
alter table test1 comment '修改后的表的注释';
3 修改字段的注释
alter table test1 modify column field_name int comment '修改后的字段注释';
--注意:字段名和字段类型照写就行
4 查看表注释的方法
--在生成的SQL语句中看
show create table test1;
--在元数据的表里面看
use information_schema;
select * from TABLES where TABLE_SCHEMA='my_db' and TABLE_NAME='test1' \G
5 查看字段注释的方法
--show
show full columns from test1;
--在元数据的表里面看
select * from COLUMNS where TABLE_SCHEMA='my_db' and TABLE_NAME='test1' \G
//取得备注 //column commend
SELECT * FROM information_schema.columns where table_name ='table_name';
//TABLE_CATALOG|TABLE_SCHEMA|TABLE_NAME|COLUMN_NAME|ORDINAL_POSITION|COLUMN_DEFAULT|IS_NULLABLE|DATA_TYPE|CHARACTER_MAXIMUM_LENGTH | CHARACTER_OCTET_LENGTH | NUMERIC_PRECISION | NUMERIC_SCALE | CHARACTER_SET_NAME | COLLATION_NAME | COLUMN_TYPE | COLUMN_KEY | EXTRA | PRIVILEGES | COLUMN_COMMENT | //table commend
SELECT TABLE_NAME, TABLE_COMMENT FROM information_schema.tables;
//| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | ENGINE | VERSION | ROW_FORMAT | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH | MAX_DATA_LENGTH | INDEX_LENGTH | DATA_FREE | AUTO_INCREMENT | CREATE_TIME | UPDATE_TIME | CHECK_TIME | TABLE_COLLATION | CHECKSUM | CREATE_OPTIONS | TABLE_COMMENT |
常见问题处理
1.Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
在链接串里加:
?zeroDateTimeBehavior=convertToNull
2.mysql 配置文件讀取順序
/etc/my.cnf
/etc/mysql/my.cnf
/usr/local/mysql/etc/my.cnf
~/.my.cnf
Linux Command
useradd -u 0 -o -g root -G root -d /home/user1 user1
passwd user1
Oracle
create user ebridge
identified by "ebridge"
default tablespace USERS
temporary tablespace TEMP
profile DEFAULT;
grant connect to ebridge;
grant dba to ebridge;
grant resource to ebridge;
grant unlimited tablespace to ebridge;