(1)mysql的启动
重启了一次服务器后,使用> mysql -u root -p登陆是出现下面的错误:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
查看状态: > /etc/rc.d/init.d/mysqld status
显示stop,未运行:>/etc/rc.d/init.d/mysqld restart
(2)mysql数据库导入、导出1.导出整个数据库
(3)MySQL数据文件目录
MySQL默认的数据文件存储目录为/var/lib/mysql,MySQL配置文件:/etc/my.cnf,MySQL启动脚本:/etc/init.d/mysqld。
(4)linux下的shell操作数据库的例子
1.shell文件内容:
#!/bin/bash
HOSTNAME="11.8.8.87"
PORT="3306"
USERNAME="root"
PASSWORD="root"
DBNAME="testa"
TABLENAME="test"
use_db_sql="use ${DBNAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "${use_db_sql}"
insert_sql="insert into ${TABLENAME} values('7','mike','123456')"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${insert_sql}"
主机地址:11.8.8.87 端口:3306 数据库:testa 表:test
2.test表结构:
3.我这里命名shell文件名为:mysqltestshell,在windows上编辑后上传到linux服务器上,
修改权限:chmod +x mysqltestshell
执行./mysqltestshell,
报错:-bash: ./mysqltestshell: /bin/bash^M: bad interpreter: No such file or directory
解决办法:
vi mysqltestshell
然后用命令 :set ff?
可以看到dos或unix的字样. 如果的确是dos格式的, 那么你可以用set ff=unix把它强制为unix格式的, 然后存盘退出. 再运行一遍看.
4.运行结果
请自觉忽略乱码情况(此处只是测试),可以看到新增了一条记录
mysql> select * from test;
+------+----------+----------------------+
| t_id | username | password |
+------+----------+----------------------+
| 1 | 娴璇 | 娴璇瀵鐮 |
| 2 | ?? | 娴璇瀵鐮 |
| 3 | ?? | 忙碌?猫炉?氓炉?莽聽? |
| 4 | ?? | ???? |
| 5 | ?? | ???? |
| 6 | ?? | ???? |
| 7 | mike | 123456 |
+------+----------+----------------------+
7 rows in set (0.00 sec)
----------------------------------------------------------补充----------------------------------------------------------------
mysql -hhostname -Pport -uusername -ppassword -e 相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作mysql的方法。
#!/bin/bash
HOSTNAME="192.168.111.84" #数据库信息
PORT="3306"
USERNAME="root"
PASSWORD=""
DBNAME="test_db_test" #数据库名称
TABLENAME="test_table_test" #数据库中表的名称
#创建数据库
create_db_sql="create database IF NOT EXISTS ${DBNAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}"
#创建表
create_table_sql="create table IF NOT EXISTS ${TABLENAME} ( name varchar(20), id int(11) default 0 )"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${create_table_sql}"
#插入数据
insert_sql="insert into ${TABLENAME} values('billchen',2)"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${insert_sql}"
#查询
select_sql="select * from ${TABLENAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"
#更新数据
update_sql="update ${TABLENAME} set id=3"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${update_sql}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"
#删除数据
delete_sql="delete from ${TABLENAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${delete_sql}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"
(4)mysql,test数据库的初始化
执行:/usr/bin/mysql_install_db,可以在/var/lib/mysql里看到数据库的目录。
(5)修改mysql登录密码
若忘记密码:
# /etc/init.d/mysqld stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD(’newpassword’) where USER=’root’;
mysql> FLUSH PRIVILEGES;
mysql> quit
# /etc/init.d/mysqld restart
# mysql -uroot -p
Enter password: <输入新设的密码newpassword>
mysql>
若记得密码,重新设置密码:
#mysql -u root -p
登录后:
mysql> UPDATE user SET Password=PASSWORD(’newpassword’) where USER=’root’;
mysql> FLUSH PRIVILEGES;
mysql> quit
取消登录密码:
mysql> update user set password='' where user='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
(6)普通用户无法登录
taiyang用户登录时,出现提示:ERROR 1045 (28000): Access denied for user 'taiyang'@'localhost' (using password: NO)
service mysqld stop
mysqld_safe --user=taiyang --skip-grant-tables --skip-networking &
然后taiyang用户就能正常登录了。