Docker deploy and use MySQL service

Docker deploy and use MySQL service

Foreword

Recently I want to use MySQLit as an experiment, so I am familiar with the familiar Dockerusage.

reference

https://hub.docker.com/_/mysql

Ready to work

# 创建相应目录
mkdir -p /tmp/mysql/data    # 用于保存数据文件(持久化)
mkdir -p /tmp/mysql/custom  # 用于保存配置文件(方式2)

# 创建配置文件1
cat > /tmp/mysql/my.cnf << EOF
[mysqld]
character-set-server=utf8
lower_case_table_names=1
[client]
default-character-set=utf8
EOF

# 创建配置文件2
cat > /tmp/mysql/custom/mycustom.cnf << EOF
[mysqld]
character-set-server=utf8
lower_case_table_names=1
[client]
default-character-set=utf8
EOF

Start MySQL by default

# 使用之前创建的配置文件1
docker run -d -e MYSQL_ROOT_PASSWORD=root \
           -v /tmp/mysql/data:/var/lib/mysql \
           -p 3306:3306 --name mysql mysql:5.6

Parameter explanation

parameter Explanation
-d The container runs in the background.
-e Set environment variables
-v Mount the host file or folder to the container for data persistence.
-p Map ports to containers.
-name Set the container name.

Start MySQL with configuration file

Choose one of two methods

Mapping host configuration files to containers

# 使用之前创建的配置文件1
docker run -d -e MYSQL_ROOT_PASSWORD=root \
           -v /tmp/mysql/my.cnf:/etc/mysql/my.cnf \
           -v /tmp/mysql/data:/var/lib/mysql \
           -p 3306:3306 --name mysql mysql:5.6

Mount the configuration file directory to the container

# 使用之前创建的配置文件2
docker run -d -e MYSQL_ROOT_PASSWORD=root \
           -v /tmp/mysql/custom:/etc/mysql/conf.d \
           -v /tmp/mysql/data:/var/lib/mysql \
           -p 3306:3306 --name mysql mysql:5.6

Start MySQL with parameters

# 只用参数不使用配置文件
docker run -d -e MYSQL_ROOT_PASSWORD=root \
           -v /tmp/mysql/data:/var/lib/mysql \
           -p 3306:3306 --name mysql \
           mysql:5.6 --character-set-server=utf8mb4 \
                     --collation-server=utf8mb4_unicode_ci \
                     --lower_case_table_names=1

Parameter explanation

parameter Explanation
–character-set-server Server character set
–collation-server Server character order
–lower_case_table_names Whether MySQL distinguishes case of table name, 0 distinguishes, 1 does not distinguish

About the character set and character order can refer to the following articles

My opinion on understanding MySQL character set and MySQL garbled characters in 5 minutes

# 查看更多可用参数
docker run -it --rm mysql:5.6 --verbose --help
# --rm 表示容器停止后会自动删除

Connect to MySQL

# 以mysql 命令行工具为例
mysql --protocol=tcp -u root -p  # 输入启动服务时用环境变量设置的密码
# 或者
mysql -h 127.0.0.1 -u root -p    # 输入启动服务时用环境变量设置的密码

Appears mysql>after the input \s, the default start-up mode and contrast configuration over the look, for example, the character set is utf8still the default latin1, we know that the configuration is working. ?

Troubleshoot

If you do not specify the connection protocol TCPor the specified host -his not the IP of the server

mysql -u root -p

The connection will report the following error

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

The reason is referred to here , the following is adapted from the forum posts:

MySQLUse Unix Socketor TCPto connect to the database for communication. The default is to -huse the option without option, that localhostis unixsocket, /tmp/mysql.sockcommunication will be carried out at this time , but the file generated by default in mysqlthe dockerimage socketis in the container /var/run/mysqld/mysqld.sock, and the socket file cannot be removed from the container Leading out, the client and server cannot communicate with the same socket file, so it is recommended to use the TCPprotocol connection.

For the socket file, please refer to Linux file type full resolution .

user settings

New user

Use to rootcreate a testuser, the password is 1234.

mysql -h 127.0.0.1 -u root -p
CREATE USER "test"@"localhost" IDENTIFIED BY "1234";  # 本地登录
CREATE USER "test"@"%" IDENTIFIED BY "1234";  # 远程登录
quit
mysql -h 127.0.0.1 -u test -p  # 测试是否创建成功

If you create an error

ERROR 1396 (HY000): Operation CREATE USER failed for 'test'@'localhost'

Check if the user already exists mysqlin the usertable in the database

select user, host from mysql.user;

Existing users need to be deleted before being created.

Authorized user

  • a. Authorization format grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码":;

  • b. Log in MySQL, log in rootas identity here :

mysql --protocol=tcp -u root -p
  • c. Create a database for the user testDB:
# 使用默认配置创建数据库
create database testDB;
# 指定编码创建数据库
create database testDB default charset utf8 collate utf8_general_ci;
  • d. Authorized testusers have testDBall the permissions of the database:
grant all privileges on testDB.* to "test"@"localhost";  # root 用户授权不需要密码
grant all privileges on testDB.* to "test"@"localhost" identified by "1234";
flush privileges;  # 刷新系统权限表
  • e. Assign some permissions to users:
grant select,update on testDB.* to "test"@"localhost" identified by "1234";
flush privileges;  # 刷新系统权限表
  • f. Authorized testusers have certain permissions for all databases:
# "%" 表示对所有非本地主机授权,不包括localhost
grant select,delete,update,create,drop on . to test@"%" identified by "1234";

Modify user

Change user password

update mysql.user set authentication_string=password("新密码") where User="test" and Host="localhost";
flush privileges;

delete users

method one

Delete FROM mysql.user Where User="test" and Host="localhost";
flush privileges;
drop database testDB;  # 如想保留数据库可省略这一步

Method two

drop user 用户名@'%';
drop user 用户名@ localhost;
Published 27 original articles · praised 4 · visits 9694

Guess you like

Origin blog.csdn.net/yoshubom/article/details/100055171