安装:
]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
]# yum -y install mysql57-community-release-el7-10.noarch.rpm
]# yum -y install mysql-community-server
]# systemctl start mysqld.service
]# systemctl status mysqld.service
]# grep "password" /var/log/mysqld.log 获取临时密码
]# mysql -uroot -p
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
授权:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
创建数据库:
mysql> create database mytest default character set utf8;
创建用户:
mysql> create user 'my_test'@'%' identified by 'My_test123';
-- %:匹配所有主机,该地方还可以设置成‘localhost’,代表只能本地访问
授权:
mysql> grant select,insert,update,delete,create on mytest.* to my_test;
--*代表整个数据库
立即启用修改:
mysql> flush privileges;
取消用户所有数据库(表)的所有权限:
mysql> revoke all on *.* from my_test;
删除用户:
mysql> delete from mysql.user where user='my_test';
删除数据库:
mysql>drop database mytest;
登陆:
mysql> mysql -u用户名 -p密码 -h地址 -P端口; --本地登陆-h -P可不要
切换数据库:
mysql> use mytest
创建表:
mysql> create table if not exists my_users(
-> userid INT UNSIGNED AUTO_INCREMENT,
-> username VARCHAR(100) NOT NULL,
-> password VARCHAR(32) NOT NULL,
-> PRIMARY KEY (userid)
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
- 如果你不想字段为 NULL 可以设置字段的属性为 NOT NULL, 在操作数据库时如果输入该字段的数据为NULL ,就会报错。
- AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1。
- PRIMARY KEY关键字用于定义列为主键。 您可以使用多列来定义主键,列间以逗号分隔。
- ENGINE 设置存储引擎,CHARSET 设置编码。
查看表:
mysql> show tables;
查看表结构:
mysql> desc my_users;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| userid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(100) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
+----------+------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
php mysqli连接1:
<?php
$dbhost = 'localhost:3306';
$dbuser = 'my_test';
$dbpw = 'My_test123';
$conn = mysqli_connect($dbhost, $dbuser, $dbpw);
if (!$conn){
die('连接失败'. mysqli_error($conn));
}
echo "连接成功!";
$sql = "SELECT * FROM my_users";
mysqli_select_db($conn, 'mytest');
$retval = mysqli_query($conn, $sql);
if(!$retval){
die('查询失败'.mysqli_error($conn));
}
$row = mysqli_fetch_array($retval, MYSQLI_ASSOC);#关联数组, MYSQLI_NUM 数字数组
print_r($row);
mysqli_free_result($retval);
mysqli_close($conn);
连接2:
<?php
$_objMysqli = new mysqli('localhost:3306', 'my_test', 'My_test123', 'mytest');
if(mysqli_connect_errno()){
echo "连接数据库失败";
exit();
}
//设置编码
$_objMysqli->set_charset('utf8');
$_sql = "SELECT * FROM my_users";
//执行SQL,把结果集赋给$_result
$_result = $_objMysqli->query($_sql);
print_r($_result);
//总共选择:num_rows
//循环获取
while($_row = $_result->fetch_assoc()){//fetch_row 数字键,fetch_assoc关联键
print_r($_row);
}
//销毁结果集
$_result->free();
$_objMysqli->close();
php pdo连接:
<?php
$mysql_conf = array(
'host' => 'localhost:3306',
'db' => 'mytest',
'db_user' => 'my_test',
'db_pw' => 'My_test123'
);
//创建一个PDO对象
$pdo = new PDO("mysql:host=".$mysql_conf['host'].
";dbname=".$mysql_conf['db'],
$mysql_conf['db_user'], $mysql_conf['db_pw']);
//设置编码
$pdo->exec("set names utf8");
$sql = "SELECT * FROM my_users";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(1, 'joshu', PDO::PARAM_STR);
$rs = $stmt->execute();
if($rs){
//PDO::FETCH_NUM 数字索引
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
print_r($row);
}
}
bin log:
开启:vim /etc/my.cnf
log-bin=mysql-bin
server-id=100
#重启服务
service mysqld restart
#进入mysql
mysql> show variables like ‘log_%’; --查看变量值
mysql> show master logs;
查看用户及可访问的host:select user,host from mysql.user;
mysql> select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| my_test | % |
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
查看数据库字符集:\s
mysql> \s
--------------
mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper
Connection id: 3
Current database: mytest
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.25-log MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 31 min 48 sec
授权远程用户:
mysql> grant all on *.* to 用户@ip地址(非mysql所在的地址,而是要连接数据库的地址) identified by password
查看日志:
mysql> flush logs; -- 刷新日志文件
mysql> show master status; -- 查看最后一个bin-log日志
mysql> reset master; -- 清空所有bin-log日志,会保留一个基本的
查看日志内容:
]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000001
恢复数据:
]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000002 |mysql -uroot -proot mytest
主从数据:
主数据库:
mysql> grant all on *.* to slave_user@192.168.207.129 identified by 'Slave_123';
从数据库:
如果虚拟机是复制过来的,还需要修改uuid;另外主数据库所在服务器的防火墙要关闭。
mysql> change master to master_host='192.168.207.128',master_port=3306,master_user='slave_user',master_password='Slave_123';
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.207.128
Master_User: slave_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000001
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: 578
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
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: 100
Master_UUID: 66bab2cf-270b-11e9-b2ff-000c2930c1c1
Master_Info_File: /var/lib/mysql/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)
mysql engine:
show engines; -- 查看所有的引擎,常用如下2种:
· MyISAM:一种非事务性的引擎,提供高速存储和检索,以及全文搜索能力,适合数据仓库等查询频繁的应用。一个table保存到3个文件当中,.frm 存储表定义,.myd存储数据,.myi存储索引。
静态MyISAM:如果数据库中的各个数据列的长度都是预先固定好的,服务器将自动选择这种表类型。(1)表中每一条记录所占用的空间都是一样的,所以存取和更新的效率非常高。(2)当数据受损时,回复工作也比较容易做。
动态MyISAM:如果数据表中出现varchar、xxxtex或xxxBLOB字段时,服务器将自动选择这种表类型。相对于静态MyISAM,这种表存储空间比较小。
压缩MyISAM:这种类型的表进一步减小了占用的存储,但是这种表压缩之后不能再被修改。
· InnoDB:支持事务性的引擎,所有的数据存储在一个或者多个数据文件中,支持类似于Oracle的锁机制。一般在OLTP应用中使用较广泛。如果没有指定InnoDB配置选项,MySQL将在MySQL数据目录下创建一个名为ibdata1的自动扩展数据文件,以及两个名为ib_logfile0和ib_logfile1的日志文件。
mysql> default-storage-engine=MyISAM;--修改默认的引擎
mysql> alter table 表名 engine=MyISAM;--修改表的引擎
https://blog.csdn.net/wjtlht928/article/details/46641865 讲的比较详细