您的位置:首页 > 数据库 > SQL

liunx架构mysql操作

2015-07-19 21:50 686 查看
mysql相关配置

[root@wangchao tmp]# vim /etc/my.cnf //mysql的配置文件
[mysqld]
port = 3306 //端口
socket = /tmp/mysql.sock //监听的sock
skip-locking //过滤lock
key_buffer_size = 256M //索引块缓存
max_allowed_packet = 1M //允许最大的包
table_open_cache = 256 //所有线程,打开表的数量
sort_buffer_size = 1M //排序缓冲区的大小
read_buffer_size = 1M //读缓冲区
read_rnd_buffer_size = 4M //随机读
myisam_sort_buffer_size = 64M //mysql引擎myisam
thread_cache_size = 8 //缓存可重用线程数
query_cache_size= 16M //查询的缓存大小
thread_concurrency = 8 //最大并发线程数

[mysqlhotcopy]
interactive-timeout=8 //断开连接时间
wait_timeout=8

log_query_time=1 //慢查询日志1S
log_slow_queries=/data/mysql/slow.log //日志路径

mysql的root密码重置
[root@wangchao tmp]# mysql -u root //默认无密码即可登入
mysql> quit
Bye
[root@wangchao tmp]# mysqladmin -u root password 'abcd' //修改root的密码
[root@wangchao tmp]# mysql -u root //无密码登入失败
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@wangchao tmp]# mysql -uroot -pabcd //使用密码登入
mysql> quit
Bye

现假设密码忘记,更改密码:
[root@wangchao tmp]# vim /etc/my.cnf
[mysqld]
skip_grant //不用授权即可登入
[root@wangchao tmp]# /etc/init.d/mysqld restart
[root@wangchao tmp]# mysql //无需密码即可登入
mysql> use mysql
mysql> update user set password=password('123') where user='root';
//更改密码为123
mysql> select *from user where user='root'\G
mysql> exit
Bye
[root@wangchao tmp]# vim /etc/my.cnf
[mysqld]
#skip_grant //注销掉,无需密码即可登入
[root@wangchao tmp]# /etc/init.d/mysqld restart //重启
[root@wangchao tmp]# mysql -uroot -p123 //可登入,密码更改为123

mysql登入
[root@wangchao ~]# mysql -uroot -p123
mysql> quit
Bye
[root@wangchao ~]# mysql -uroot -h192.168.137.22 -P3306 -p123
ERROR 1130 (HY000): Host '192.168.137.22' is not allowed to connect to this MySQL server
//不通
[root@wangchao ~]# telnet 192.168.137.22 3306
//查看该端口是否通,不通因为未授权
[root@wangchao ~]# mysql -uroot -h127.0.0.1 -P3306 -p123 //可登入
mysql>
mysql> grant all on *.* to 'root'@'192.168.137.22' identified by '123aaa';
mysql> use mysql;
mysql> select * from user where host='192.168.137.22';
mysql> select * from user where host='192.168.137.22'\G; //查看授权成功

[root@wangchao ~]# mysql -uroot -h192.168.137.22 -P3306 -p123aaa
mysql> //可登入可远程登入mysql
mysql> select user(); //查看当前登入用户
+---------------------+
| user() |
+---------------------+
| root@192.168.137.22 |
+---------------------+
1 row in set (0.00 sec)

[root@wangchao ~]# mysql -uroot -S /tmp/mysql.sock -p
//本地存在多个mysql,用sock登入
Enter password:
mysql>

mysql常用操作:
[root@wangchao ~]# mysql -uroot -p123 //登入
mysql>
mysql> show databases;
mysql> use mysql
mysql> use discuz
mysql> select database();
mysql> select user();
mysql> select version();
mysql> use discuz
mysql> show tables;
mysql> desc pre_ucenter_vars;
mysql> show create table pre_ucenter_vars\G;
mysql> desc pre_forum_post;
mysql> create database wang;
mysql> use wang;
mysql> create table tb1 (`id` int(4), `name` char(40)) ENGINE=MyISAM DEFAULT CHARSET=gbk;
mysql> show tables;
mysql> desc tb1;
mysql> show create table tb1\G;
mysql> insert into tb1 values(1,'wang');
mysql> select *from tb1;
mysql> insert into tb1 values(2,'cai');
mysql> select *from tb1;
+------+------+
| id | name |
+------+------+
| 1 | wang |
| 2 | cai |
+------+------+
mysql> insert into tb1 (`id`) values(2);
mysql> insert into tb1 (`name`) values('4');

mysql> select *from tb1;
+------+------+
| id | name |
+------+------+
| 1 | wang |
| 2 | cai |
| 2 | NULL |
| NULL | 4 |
+------+------+

mysql> insert into tb1(`name`,`id`) values('55',6);
mysql> select *from tb1;
+------+------+
| id | name |
+------+------+
| 1 | wang |
| 2 | cai |
| 2 | NULL |
| NULL | 4 |
| 6 | 55 |
+------+------+
5 rows in set (0.00 sec)

mysql> update tb1 set id=5 where name='55'
mysql> select *from tb1;
+------+------+
| id | name |
+------+------+
| 1 | wang |
| 2 | cai |
| 2 | NULL |
| NULL | 4 |
| 5 | 55 |
+------+------+
5 rows in set (0.00 sec)

mysql> truncate table wang.tb1
mysql> drop table tb1;
mysql> drop database wang;
mysql> show databases;

mysql常用操作2

mysql> grant all on discuz .* to 'user1'@'192.168.137.%' identified by 'ccc';
mysql> flush privileges;
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 12 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

mysql> show variables;
mysql> set global max_connections=200;
mysql> show variables like 'max_connec%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 10 |
| max_connections | 200 |
+--------------------+-------+
2 rows in set (0.00 sec)

mysql> set global max_connec_errors=100;
mysql> show variables like 'max_connec%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
| max_connections | 200 |
+--------------------+-------+
2 rows in set (0.00 sec)

mysql> show status;
mysql> show status like '%running';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| Slave_running | OFF |
| Threads_running | 1 |
+-----------------+-------+
2 rows in set (0.00 sec)

mysql> quit
[root@wangchao ~]# vim /etc/init.d/mysqld //查看日志文件
basedir=/usr/local/mysql
datadir=/data/mysql
[root@wangchao ~]# cd /data/mysql/
[root@wangchao mysql]# ls
wangchao.err
[root@wangchao mysql]# tail wangchao.err
[root@wangchao mysql]# mysql -uroot -p123
mysql> repair table discuz.pre_forum_post; //修复表
+-----------------------+--------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-----------------------+--------+----------+----------+
| discuz.pre_forum_post | repair | status | OK |
+-----------------------+--------+----------+----------+
1 row in set (0.04 sec)

mysql备份操作
[root@wangchao ~]# mysqldump -uroot -p123 discuz //查看备份内容
[root@wangchao ~]# mysqldump -uroot -p123 discuz >/data/discuz.sql
[root@wangchao ~]# vim /data/discuz.sql
[root@wangchao ~]# cd /data/mysql/
[root@wangchao mysql]# ls
[root@wangchao mysql]# cd discuz/
[root@wangchao discuz]# rm -rf pre_forum_post*
//删除数据表,备份库
[root@wangchao discuz]# mysql -uroot -p123 discuz </data/discuz.sql
//恢复数据,恢复库

[root@wangchao discuz]# mysqldump -uroot -p123 discuz pre_forum_post >/data/post.sql;
//备份一个表
[root@wangchao discuz]# cat /data/post.sql
[root@wangchao discuz]# mysql -uroot -p123 discuz </data/post.sql
//恢复一个表
[root@wangchao discuz]# mysqldump -uroot --default-character-set=gbk -p123 discuz pre_forum_post >/data/post.sql
//备份(一个表)时指定字符集,指定字符集防止一些出错的问题
[root@wangchao discuz]# mysqldump -uroot --default-character-set=gbk -p123 discuz pre_forum_post </data/post.sql
//恢复
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息